Noob questions about Python

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Wed Oct 17 23:32:59 CEST 2007


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.




More information about the Python-list mailing list