How can I reverse a list in python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

How can I do this in python?

array = [0,10,20,40]
for (i = array.length() - 1 ;i >= 0; i--)

I need to have the elements of an array but from the end to the beginning.

Answers

http://docs.python.org/release/2.4.4/whatsnew/node7.html http://docs.python.org/release/2.4.4/whatsnew/node7.html

>>> array=[0,10,20,40]
>>> for i in reversed(array):
...     print i

Note that reversed(...) does not return a list. You can get a reversed list using list(reversed(array)).

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/3940128/how-can-i-reverse-a-list-in-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils