using reverse in list of tuples

Steven W. Orr steveo at syslang.net
Thu Jun 10 11:51:40 EDT 2010


On 06/10/10 04:41, quoth Marco Nawijn:
> On Jun 10, 2:39 am, james_027 <cai.hai... at gmail.com> wrote:
>> hi,
>>
>> I am trying to reverse the order of my list of tuples and its is
>> returning a None to me. Is the reverse() function not allow on list
>> containing tuples?
>>
>> Thanks,
>> James
> 
> As the others already mentioned list.reverse() is in-place, just as
> for
> example list.sort(). Alternatively, use the builtin reversed() or
> sorted()
> functions to get a return value (they will leave the original list
> unmodified.
> 
> Example:
>>>> a = range(3)
>>>> b = reversed(a)
>>>> for item in b:
>       ...print item
> This will produce:
>   2
>   1
>   0
> 
> Note that reversed returns an iterator.
> 
> Marco

How about just doing it the old fashioned way via slicing.

>>> foo = (4,5,8,2,9,1,6)
>>> foo[::-1]
(6, 1, 9, 2, 8, 5, 4)
>>>

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 261 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100610/6277d9f2/attachment-0001.sig>


More information about the Python-list mailing list