470,570 Members | 2,252 Online

# How to reverse tuples in a list?

I have a list of tuples
[('a', 1.0), ('b', 2.0), ('c', 3.0)]
I want to reverse the order of the elements inside the tuples.
[(1.0,'a'), (2.0, 'b'), (3.0, 'c')]

I know I could do this long-form:
q = []
y = [('a', 1.0), ('b', 2.0), ('c', 3.0)]
for i in y:
t=list(t)
t.reverse()
q.append(tuple(t))
y = q

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.

Yours,
Noah

Aug 8 '06 #1
11 16566 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 && ma*@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
Aug 8 '06 #2
Noah wrote:
I have a list of tuples
[('a', 1.0), ('b', 2.0), ('c', 3.0)]
I want to reverse the order of the elements inside the tuples.
[(1.0,'a'), (2.0, 'b'), (3.0, 'c')]

I know I could do this long-form:
q = []
y = [('a', 1.0), ('b', 2.0), ('c', 3.0)]
for i in y:
t=list(t)
t.reverse()
q.append(tuple(t))
y = q

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.

Yours,
Noah
Provided the data remains the same [(a, b), ...]
Python 2.5a2 (r25a2:45740, May 24 2006, 19:50:20)
[GCC 3.3.6] on linux2
>>x = [('a', 1.0), ('b', 2.0), ('c', 3.0)]
y = [(b, a) for a, b in x]
y
[(1.0, 'a'), (2.0, 'b'), (3.0, 'c')]
Hope this helps,
Aug 9 '06 #3
Noah wrote:
I have a list of tuples
[('a', 1.0), ('b', 2.0), ('c', 3.0)]
I want to reverse the order of the elements inside the tuples.
[(1.0,'a'), (2.0, 'b'), (3.0, 'c')]

I know I could do this long-form:
q = []
y = [('a', 1.0), ('b', 2.0), ('c', 3.0)]
for i in y:
t=list(t)
t.reverse()
q.append(tuple(t))
y = q

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.

Yours,
Noah
If your tuples are all two items, you can do it like this:

y = [(b, a) for a, b in y]

if not, then:

y = [tuple(reversed(t)) for t in y]

:-D

Peace,
~Simon

Aug 9 '06 #4
Noah wrote:
I have a list of tuples
[('a', 1.0), ('b', 2.0), ('c', 3.0)]
I want to reverse the order of the elements inside the tuples.
[(1.0,'a'), (2.0, 'b'), (3.0, 'c')]
Python 2.4+:

y = [tuple(reversed(t)) for t in y]

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
an underlying truth."
-- Umberto Eco

Aug 9 '06 #5
Erik Max Francis wrote:
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?
Because he has a list of tuples and wants to reverse each individual tuple in
the list.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
an underlying truth."
-- Umberto Eco

Aug 9 '06 #6
"Noah" <no**@noah.orgwrote in message
I have a list of tuples
[('a', 1.0), ('b', 2.0), ('c', 3.0)]
I want to reverse the order of the elements inside the tuples.
[(1.0,'a'), (2.0, 'b'), (3.0, 'c')]
>>tups = [('a', 1.0), ('b', 2.0), ('c', 3.0)]
map(tuple,map(reversed,tups))
[(1.0, 'a'), (2.0, 'b'), (3.0, 'c')]
Aug 9 '06 #7
"Noah" <no**@noah.orgwrites:
I know I could do this long-form:
q = []
y = [('a', 1.0), ('b', 2.0), ('c', 3.0)]
...
y = [tuple(reversed(t)) for t in y]
Aug 9 '06 #8
Noah wrote:
I have a list of tuples
[('a', 1.0), ('b', 2.0), ('c', 3.0)]
I want to reverse the order of the elements inside the tuples.
[(1.0,'a'), (2.0, 'b'), (3.0, 'c')]
Python 2.4.x :
y = [tuple(reversed(t)) for t in y]

Python < 2.4.x:

def reverse_tuple(t):
t = list(t)
t.reverse()
return tuple(t)

y = [reverse_tuple(t) for t in y]

HTH
Aug 9 '06 #9
Applying the perl motto (there is more than one way to do it) sure enough leads
to a perlish solution... as measured by line noise.
>>t = [('a', 11,1.0), ('b',22,2.0),('c',33,3.0)]
zip(*zip(*t)[::-1])
[(1.0, 11, 'a'), (2.0, 22, 'b'), (3.0, 33, 'c')]

Aug 9 '06 #10
Robert Kern <ro*********@gmail.comwrote:
>Python 2.4+:

y = [tuple(reversed(t)) for t in y]
Python 2.3:

y = [ t[::-1] for t in y ]

Obviously works in 2.4 as well, where I make it faster than using
tuple(reversed(t)). Which isn't surprising, as it's not constructing
the intermediate list.

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Aug 9 '06 #11
Noah wrote (among other things ;)):
I have a list of tuples
I want to reverse the order of the elements inside the tuples.
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.
This is a minor point, but I've got the impression you're not completely
aware that tuples are immutable, and what that implies. 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. This is not what tuples
are for, so there.

Sorry if you knew this, just wanted to point out that there is a
conscious difference between tuples and lists.
This is of course just a half-newbie crackin wise, so forgive me ;).

wildemar

Aug 10 '06 #12

### This discussion thread is closed

Replies have been disabled for this discussion.