How to Reverse List Elements in Python?
Hello Guys,
In this tutorial, we will go over the demonstration of how to reverse list in python. I explained simply step by step python reverse list example. If you have a question about python list sort reverse example then I will give a simple example with a solution. you will learn python reverse list and return. So, let us dive into the details.
There are many ways to reverse list items in python. i will give you two examples using [::-1] and reversed() function to reverse list elements in python. so let's see the below examples.
You can use these examples with python3 (Python 3) version.
let's see below a simple example with output:
Example 1:
main.py
myList1 = [1, 2, 3, 4, 5] myList2 = ["One", "Two", "Three", "Four", "Five"] # Python List Reverse Code list1Reversed = myList1[::-1] list2Reversed = myList2[::-1] print(list1Reversed) print(list2Reversed)
Output:
[5, 4, 3, 2, 1] ['Five', 'Four', 'Three', 'Two', 'One']
Example 2:
main.py
myList1 = [1, 2, 3, 4, 5] myList2 = ["One", "Two", "Three", "Four", "Five"] # Python List Reverse Code list1Reversed = list(reversed(myList1)) list2Reversed = list(reversed(myList2)) print(list1Reversed) print(list2Reversed)
Output:
[5, 4, 3, 2, 1] ['Five', 'Four', 'Three', 'Two', 'One']
I hope it can help you...