p>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']
My name is Divyang Vadodariya. I'm a full-stack developer, entrepreneur and owner of CodeTheTrack. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap ,etc from the early stage.
Copyright © 2023 www.CodeTheTrack.com • All Rights Reserved