By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

list.reverse()

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
>
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.