How can I do the following 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.
How can I do the following 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.
Use list comprehension:
ORGANIZING VALUES:
In Python, lists' order too can be manipulated with sort, organizing your variables in numerical/alphabetical order:
Temporarily:
Permanent:
You can sort with the flag "reverse=True":
WITHOUT ORGANIZING
Maybe you do not want to sort values, but only reverse the values. Then we can do it like this:
**Numbers have priority over alphabet in listing order. The Python values' organization is awesome.
You could always treat the list like a stack just popping the elements off the top of the stack from the back end of the list. That way you take advantage of first in last out characteristics of a stack. Of course you are consuming the 1st array. I do like this method in that it's pretty intuitive in that you see one list being consumed from the back end while the other is being built from the front end.
Extended slice syntax is explained well in the Python What's new Entry for release
2.3.5
By special request in a comment this is the most current slice documentation.