From: james_027 on
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
From: rantingrick on
On Jun 9, 7:39 pm, james_027 <cai.hai...(a)gmail.com> wrote:
>
> 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?


list.revese() is an in-place operation! don't try to assign the return
value back to your list!

>>> lst = [(x, x+1) for x in range(5)]
>>> lst
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
>>> lst.reverse()
>>> lst
[(4, 5), (3, 4), (2, 3), (1, 2), (0, 1)]
From: Terry Reedy on
On 6/9/2010 8:39 PM, james_027 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?

No. Mutation methods of builtins generally return None.


From: Marco Nawijn on
On Jun 10, 2:39 am, james_027 <cai.hai...(a)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
From: Steven W. Orr on
On 06/10/10 04:41, quoth Marco Nawijn:
> On Jun 10, 2:39 am, james_027 <cai.hai...(a)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