2 Ways to Reverse a List in Python 3

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
- Python 3: Formatting a DateTime Object as a String
- Python 3: Convert Datetime to Timestamp and vice versa
- How to Check Whether an Object is Iterable in Python 3
- How to Install Python Libraries in Google Colab
- Examples of using map() function in Python 3
You can also check out our Machine Learning category page or Python category page for more tutorials and examples.
Subscribe
0 Comments