How to reverse tuples in a list?

N

Noah

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
 
E

Erik Max Francis

Noah said:
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:
(3, 2, 1)
 
R

Robert Kern

Noah said:
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
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
A

Adonis

Noah said:
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,
Adonis
 
R

Robert Kern

Erik said:
Noah said:
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
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
S

Simon Forman

Noah said:
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
 
B

Bruno Desthuilliers

Noah said:
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
 
S

Sion Arrowsmith

Robert Kern said:
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.
 
W

Wildemar Wildenburger

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top