Re: How to reverse tuples in a list?



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@xxxxxxxxxxx && 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
.



Relevant Pages

  • Re: How to reverse tuples in a list?
    ... I want to reverse the order of the elements inside the tuples. ... I know I could do this long-form: ... list method and because it operates ... it in a list comprehension. ...
    (comp.lang.python)
  • Re: How to reverse tuples in a list?
    ... I want to reverse the order of the elements inside the tuples. ... I know I could do this long-form: ... list method and because it operates ... it in a list comprehension. ...
    (comp.lang.python)
  • Re: How to reverse tuples in a list?
    ... I want to reverse the order of the elements inside the tuples. ... it in a list comprehension. ... I mean that t.reverse, assuming the same semantics as the list method, is just not possible, as it would try to alter the tuple. ... just wanted to point out that there is a conscious difference between tuples and lists. ...
    (comp.lang.python)
  • How to reverse tuples in a list?
    ... I want to reverse the order of the elements inside the tuples. ... I know I could do this long-form: ... list method and because it operates ... it in a list comprehension. ...
    (comp.lang.python)