I've always found the reverse() method of a list rather wierd because it reverses the list in place. I just found out about reversing via slicing:
>>> f=['foo', 'bar', 'baz'] >>> f[::-1] ['baz', 'bar', 'foo'] >>> f='hello' >>> f[::-1] 'olleh'
There is also a new builtin function in 2.4 called reversed(), which returns an iterator, and is more efficient for large lists:
>>> f=['foo', 'bar', 'baz'] >>> for x in reversed(f): ... print x ... baz bar foo
And yet another (ugly) method shown to me by a coworker:
>>> f=['foo', 'bar', 'baz'] >>> reduce(lambda x,y: [y] + ((type(x) == type([])) ... and x or [x]), f) ['baz', 'bar', 'foo']
Hmm, Python seems to be moving towards TMTOWTDI.
No Comments/Pingbacks for this post yet...
Donate to keep this site going!
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|
| << < | > >> | |||||
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | |||