Sign up or log in to customize your list.
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.
How can I reverse a list in Python?
How can I do the following in Python?
I need to have the elements of an array, but from the end to the beginning.
Note that reversed(...) does not return a list. You can get a reversed list using list(reversed(array)).
By special request in a comment this is the most current slice documentation.
The job is done, and now you have a reversed list.
Using slicing, e.g. array = array[::-1], is a neat trick and very Pythonic, but a little obscure for newbies maybe. Using the reverse() method is a good way to go in day to day coding because it is easily readable.
# Now test it!!
Done!
Note that this will not work on Tuples or string sequences, because strings and tuples are immutable, i.e., you cannot write into them to change elements.
I find (contrary to some other suggestions) that l.reverse() is by far the fastest way to reverse a long list in Python 3 and 2. I'd be interested to know if others can replicate these timings.
l[::-1] is probably slower because it copies the list prior to reversing it. Adding the list() call around the iterator made by reversed(l) must add some overhead. Of course if you want a copy of the list or an iterator then use those respective methods, but if you want to just reverse the list then l.reverse() seems to be the fastest way.
Using reversed(array) would be the likely best route.
Should you need to understand how could implement this without using the built in reversed.
This should take O(N) time.
If you want to store the elements of reversed list in some other variable, then you can use revArray = array[::-1] or revArray = list(reversed(array)).
**Numbers have priority over alphabet in listing order. The Python values' organization is awesome.
Strictly speaking, the question is not how to return a list in reverse but rather how to reverse a list with an example list name array.
To reverse a list named "array" use array.reverse().
The incredibly useful slice method as described can also be used to reverse a list in place by defining the list as a sliced modification of itself using array = array[::-1].
Whatever you do, don't do it this way.
This is rather cryptic but may be useful.
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.
Using some old school logic to practice for interviews.
...
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged python list or ask your own question.
The Ask Question Wizard is Live!
How to reverse a list of lists in python?
how can i reverse a list of array in python?
How to merge two dictionaries in a single expression?
What does the “yield” keyword do?
Accessing the index in 'for' loops?
How do I sort a dictionary by value?
How to make a chain of function decorators?
How do I list all files of a directory?
How to read a file line-by-line into a list?
Is it possible for SQL statements to execute concurrently within a single session in SQL Server?
What are the diatonic extended chords of C major?
Is there any word for a place full of confusion?
How do I use the new nonlinear finite element in Mathematica 12 for this equation?
What was the first language to use conditional keywords?
Time to Settle Down!
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
How does the math work when buying airline miles?
How does the secondary effect of the Heat Metal spell interact with a creature resistant/immune to fire damage?
What is this clumpy 20-30cm high yellow-flowered plant?
Is CEO the "profession" with the most psychopaths?
Is it a good idea to use CNN to classify 1D signal?
How does Python know the values already stored in its memory?
How often does castling occur in grandmaster games?
What order were files/directories outputted in dir?
How to play a character with a disability or mental disorder without being offensive?
How do living politicians protect their readily obtainable signatures from misuse?
Do wooden building fires get hotter than 600°C?
Is grep documentation about ignoring case wrong, since it doesn't ignore case in filenames?
What initially awakened the Balrog?
What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?
