How to reverse tuples in a list?

Robert Kern robert.kern at gmail.com
Tue Aug 8 20:13:16 EDT 2006


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




More information about the Python-list mailing list