Re: Noob questions about Python



Ixiaus wrote:

val = 'string'
li = list(val)
print li.reverse()

returns nothing, but,

Yes -- li.reverse() returns None. "print None" prints nothing.

val = 'string'
li = list(val)
li.reverse()
print li

returns what I want.

I'm afraid not. li.reverse() still returns None, but this time you
print li, and this shows the reversed list.

As already explained, li.reverse() modifies the existing list. What
to use here depends on what you want to achieve:

If you really want to reverse the list (i. e. re-sort the list in
memory, and perhaps work with it afterwards), use "li.reverse()".

If you just want to have all its members in reverse order, use the
builtin reversed(li) (which is an iterator giving you li's members
from back to front when iterating over it).

Regards,


Björn

--
BOFH excuse #243:

The computer fleetly, mouse and all.

.



Relevant Pages

  • Re: Runaway Prius in California
    ... cop told him to put it in neutral but he was "afraid i ... would put it in reverse by mistake". ... Did he really say those exact words, "afraid I would put it in reverse by ...
    (rec.gambling.poker)
  • Re: Big Endian Byte Ordering
    ... so addressing to structure members is not affected by big-endian byte ... Jonathan Wood ... >> I know that with, for example, a WORD value, big endian byte ordering ... >> and I don't know if I can simply reverse all the bytes, ...
    (microsoft.public.vc.mfc)
  • Re: Big Endian Byte Ordering
    ... so addressing to structure members is not affected by big-endian byte ... Jonathan Wood ... >> I know that with, for example, a WORD value, big endian byte ordering ... >> and I don't know if I can simply reverse all the bytes, ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Big Endian Byte Ordering
    ... so addressing to structure members is not affected by big-endian byte ... Jonathan Wood ... >> I know that with, for example, a WORD value, big endian byte ordering ... >> and I don't know if I can simply reverse all the bytes, ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Run away cars
    ... because he was afraid it "might slip into reverse". ... their lives and the lives of others, ... If they didn't stop their car because they were afraid of damaging it, ...
    (alt.home.repair)