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.
I think that the best way to reverse a list in Python is to do:
The job is done, and now you have a reversed list.
Use the reversed function as follow and print it
This is to duplicate the list:
This is to reverse the list in-place:
You can also use the bitwise complement of the array index to step through the array in reverse:
Whatever you do, don't do it this way.
With
reversed
andlist
: