Send Close Add comments: (status displays here)
Got it!  This site "www.robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Python: Processing lists in reverse order
by RS  admin@robinsnyder.com : 1024 x 640


1. Python: Processing lists in reverse order
There are many ways to process a Python list in reverse order. Consider the following list.

The code examples will output this list in reverse order. The code examples create a new list2 that is the list list1 reversed. Output is done in the same way.


2. Make a new list
This way makes a new list by traversing the list in reverse order. This is not the best way to do this.

Here is the Python code [#3]

Here is the output of the Python code.


3. List comprehension
Another way is to reverse the original list to a new list using a list comprehension.

Here is the Python code [#4]

Here is the output of the Python code.

One can directly print the list using a list comprehension.

Here is the Python code [#5]

Here is the output of the Python code.


4. Reversed
There is a reversed function that can do this efficiently.

Here is the Python code [#6]

Here is the output of the Python code.


5. Combining everything
Here is a short way to do this.

Here is the Python code [#7]

Here is the output of the Python code.


by RS  admin@robinsnyder.com : 1024 x 640