Reverse List Elements in Python Example

12-Dec-2022

.

Admin

Reverse List Elements in Python Example

Hi Dev,

This article will provide some of the most important example reverse list elements in python example. we will help you to give example of how to reverse list elements in python. I’m going to show you about how to reverse list items in python. We will look at example of python reverse list items.

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.

so let's see following examples 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']

#Python