473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

list.reverse()

This set of codes works:
>>x = range(5)
x.reverse()
x
[4, 3, 2, 1, 0]

But this doesn't:
>>x = range(5).reverse()
print x
None

Please explain this behavior. range(5) returns a list from 0 to 4 and
reverse just reverses the items on the list that is returned by
range(5). Why is x None (null)?
Jun 27 '08 #1
9 4149
Mark Bryan Yu <va****@gmail.comwrites:
This set of codes works:
>>>x = range(5)
x.reverse()
x
[4, 3, 2, 1, 0]

But this doesn't:
>>>x = range(5).reverse()
print x
None

Please explain this behavior. range(5) returns a list from 0 to 4 and
reverse just reverses the items on the list that is returned by
range(5). Why is x None (null)?
have you tried typing help(list.reverse) at the interactive prompt?

--
Arnaud
Jun 27 '08 #2
Mark Bryan Yu a écrit :
This set of codes works:
>>>x = range(5)
x.reverse()
x
[4, 3, 2, 1, 0]

But this doesn't:
>>>x = range(5).reverse()
print x
None
This works just as expected - at least for anyone having read the doc.
Please explain this behavior. range(5) returns a list from 0 to 4 and
reverse just reverses the items on the list that is returned by
range(5). Why is x None (null)?
Because that's what list.reverse() returns. Call it a wart if you want
(FWIW, I do), but at least that's well documented.
Jun 27 '08 #3
Roy Smith a écrit :
(snip)
The reasoning goes along the lines of, "reverse in place is an expensive
operation, so we don't want to make it too easy for people to do". At
least that's the gist of what I got out of the argument the many times it
has come up.
IIRC, it's more along the line of "reverse in place is a *destructive*
operation, so we don't want to make it too easy for people to forget
about it".

Jun 27 '08 #4
On Apr 28, 1:12*pm, Mark Bryan Yu <vaf...@gmail.comwrote:
This set of codes works:
>x = range(5)
x.reverse()
x

[4, 3, 2, 1, 0]
You can also use list slicing to get a reversed list:
>>x = range(5)
x
[0, 1, 2, 3, 4]
>>x[::-1]
[4, 3, 2, 1, 0]

-- Paul
Jun 27 '08 #5
>
The reasoning goes along the lines of, "reverse in place is an expensive
operation, so we don't want to make it too easy for people to do". At
least that's the gist of what I got out of the argument the many times it
has come up.

It's not about the storage - it is about the in-place-modification. The
reasioning is that people otherwise could assume that

a = [1, 2]
b = a.reverse()
assert a[0] == 1 and b[0] == 2

would hold, but instead of course "a" is changed.

Using reversed as

b = reversed(a)

will make that assumption hold.

Diez
Jun 27 '08 #6
On Tue, 29 Apr 2008 07:26:07 -0700, Paul McGuire wrote:
On Apr 28, 1:12Â*pm, Mark Bryan Yu <vaf...@gmail.comwrote:
>This set of codes works:
>>x = range(5)
x.reverse()
x

[4, 3, 2, 1, 0]

You can also use list slicing to get a reversed list:
>>>x = range(5)
x
[0, 1, 2, 3, 4]
>>>x[::-1]
[4, 3, 2, 1, 0]

-- Paul
More alternatives:
>>range(4, -1, -1)
[4, 3, 2, 1, 0]
>>list(reversed(xrange(5)))
[4, 3, 2, 1, 0]

If you don't need a list the fastest thing will be
xrange(4, -1, -1)

--
Ivan
Jun 27 '08 #7
On Apr 29, 9:32 am, Roy Smith <r...@panix.comwrote:
The reasoning goes along the lines of, "reverse in place is an expensive
operation, so we don't want to make it too easy for people to do". At
least that's the gist of what I got out of the argument the many times it
has come up.
Except reversing a list in place isn't expensive at all. I assume you
meant "reverse NOT in-place".

You're pretty much right but you're missing a step. That an in-place
operation is cheaper is the main reason why reverse is destructive,
that is true. But it returns None mostly to avoid confusing the user,
who is likely to assume that the operation is non-destructive (or
forget that it isn't).
Carl Banks
Jun 27 '08 #8
In article
<98**********************************@24g2000hsh.g ooglegroups.com>,
blaine <fr*****@gmail.comwrote:

Check out this cool little trick I recently learned:
>x=range(5)
x.reverse() or x
[4, 3, 2, 1, 0]

Useful for returning lists that you need to sort or reverse without
wasting that precious extra line :)

What it does: x.reverse() does the reverse and returns None. or is
bitwise, so it sees that 'None' is not 'True' and then continues to
process the next operand, x. x or'd with None will always be x (and x
has just been changed by the reverse()). So you get the new value of
x :)
Please don't do that in any code I have to read and understand. Cool
little tricks have no place in good code.
>>x = range(5)
x.reverse()
x
[4, 3, 2, 1, 0]

does the same thing, and it a lot easier to understand. I buy my newlines
in the big box at Costo, so I don't mind using a few extra ones here or
there.
Jun 27 '08 #9
On Apr 29, 8:51 pm, Roy Smith <r...@panix.comwrote:
In article
<98c4ad4d-3174-40cd-b281-84e318d69...@24g2000hsh.googlegroups.com>,

blaine <frik...@gmail.comwrote:
Check out this cool little trick I recently learned:
>>x=range(5)
>>x.reverse() or x
[4, 3, 2, 1, 0]
Useful for returning lists that you need to sort or reverse without
wasting that precious extra line :)
What it does: x.reverse() does the reverse and returns None. or is
bitwise, so it sees that 'None' is not 'True' and then continues to
process the next operand, x. x or'd with None will always be x (and x
has just been changed by the reverse()). So you get the new value of
x :)

Please don't do that in any code I have to read and understand. Cool
little tricks have no place in good code.
>x = range(5)
x.reverse()
x

[4, 3, 2, 1, 0]

does the same thing, and it a lot easier to understand. I buy my newlines
in the big box at Costo, so I don't mind using a few extra ones here or
there.
haha true - i usually don't use shortcuts, it kind of defeats the
purpose of the readability of python :)
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: M. Clift | last post by:
Hi All, Could someone help me out with this? items = ('a', 'b', 'c', 'd') items + 1 = ( 'b', 'c', 'd', 'a') items + 2 = ( 'c', 'd', 'a', 'b') items + 3 = ( 'd', 'a', 'b', 'c') trans = 1
41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
33
by: junky_fellow | last post by:
Consider a singly-linked list. Each node has a structure, struct node { char c; struct node *next; }; Each of the nodes contain an alphabetic character. Can anybody suggest a way to print...
11
by: Neo | last post by:
Hi Frns, Could U plz. suggest me how can i reverse a link list (without recursion)??? here is my code (incomplete): #include<stdio.h>
4
by: MJ | last post by:
Hi I have written a prog for reversing a linked list I have used globle pointer Can any one tell me how I can modify this prog so that I dont have to use extra pointer Head1. When I reverse a LL...
11
by: Noah | last post by:
I have a list of tuples I want to reverse the order of the elements inside the tuples. I know I could do this long-form: q = y = for i in y: t=list(t)
17
by: John Salerno | last post by:
Hi everyone. If I have a list of tuples, and each tuple is in the form: (year, text) as in ('1995', 'This is a citation.') How can I sort the list so that they are in chronological order based...
20
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with 5 keys. dict = {1: , 2: , 900000: , 900001:...
6
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.