Python Reverse List Elements Example

Published On: 19/12/2022 | Category: Python


Hi Guys,

In this tutorial is focused on how to reverse list elements in python. let’s discuss about python reverse element in list example. i’m going to show you about how to reverse list elements in python in python.

Python has a variety of methods for reversing list items. I'll give you two examples of how to reverse list members in Python using [::-1] and the reversed() method. So let's look at the samples below.

So let's see bellow example:

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']

It will help you....

Happy Pythonic Coding!