Re: Noob questions about Python
- From: Bjoern Schliessmann <usenet-mail-0306.20.chr0n0ss@xxxxxxxxxxxxxxx>
- Date: Wed, 17 Oct 2007 23:32:59 +0200
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.
.
- References:
- Noob questions about Python
- From: Ixiaus
- Noob questions about Python
- Prev by Date: Re: Pull Last 3 Months
- Next by Date: Re: Appending a list's elements to another list using a list comprehension
- Previous by thread: Re: Noob questions about Python
- Next by thread: Re: Noob questions about Python
- Index(es):
Relevant Pages
|