list.reverse()

Ivan Illarionov ivan.illarionov at gmail.com
Tue Apr 29 15:15:16 EDT 2008


On Tue, 29 Apr 2008 07:26:07 -0700, Paul McGuire wrote:

> On Apr 28, 1:12 pm, Mark Bryan Yu <vaf... at gmail.com> wrote:
>> This set of codes works:
>>
>> >>> x = range(5)
>> >>> x.reverse()
>> >>> x
>>
>> [4, 3, 2, 1, 0]
>>
>>
> You can also use list slicing to get a reversed list:
> 
>>>> x = range(5)
>>>> x
> [0, 1, 2, 3, 4]
>>>> x[::-1]
> [4, 3, 2, 1, 0]
> 
> -- Paul

More alternatives:

>>> range(4, -1, -1)
[4, 3, 2, 1, 0]

>>> list(reversed(xrange(5)))
[4, 3, 2, 1, 0]

If you don't need a list the fastest thing will be
xrange(4, -1, -1)

-- 
Ivan



More information about the Python-list mailing list