2 Ways to Reverse a List in Python 3

Last updated on November 10, 2021 Pennywise Loading... Post a comment

This concise article shows you 2 ways to reverse a given list in Python 3. The first approach is to use slice notation and the second one is to use the reversed() function. Without any further ado, let’s see some code.

Using slice notation

Example:

list1 = [1, 2, 3, 4, 5]
list2 = ['A', 'B', 'C']

list1_reversed = list1[::-1]
list2_reversed = list2[::-1]

print(list1_reversed)
print(list2_reversed)

Output:

[5, 4, 3, 2, 1]
['C', 'B', 'A']

Using the reversed() function

Example:

list1 = [1, 2, 3, 4, 5]
list2 = ['A', 'B', 'C']

list1_reversed = list(reversed(list1))
list2_reversed = list(reversed(list2))

print(list1_reversed)
print(list2_reversed)

Output:

[5, 4, 3, 2, 1]
['C', 'B', 'A']

Further reading

You can also check out our Machine Learning category page or Python category page for more tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You May Also Like