How to reverse tuples in a list?
Erik Max Francis
max at alcyone.com
Tue Aug 8 20:03:17 EDT 2006
Noah wrote:
> But it seems like there should be a clever way to do this with
> a list comprehensions. Problem is I can't see how to apply
> reverse() to each tuple in the list because reverse() a
> list method (not a tuple method) and because it operates
> in-place (does not return a value). This kind of wrecks doing
> it in a list comprehension. What I'd like to say is something like
> this:
> y = [t.reverse() for t in y]
> Even if reverse worked on tuples, it wouldn't work inside a
> list comprehension.
Why would you want to do it with list comprehensions? Use reversed:
>>> t = (1, 2, 3)
>>> u = tuple(reversed(t))
>>> u
(3, 2, 1)
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
The quickest way of ending a war is to lose it.
-- George Orwell, 1903-1950
More information about the Python-list
mailing list