Python List - Reverse - Using reverse(), slicing
Python - Reverse a List
To reverse the order of items in a list, or simply said reverse a list, use reverse() method of List Class.
In this tutorial, we shall first discuss about the trivial reverse() function to reverse a Python List. Then we shall go through some of the unconventional approaches to reverse a list, for widening our Python knowledge.
Syntax of reverse()
The syntax of reverse() method is given below.
mylist.reverse()
Another method of reversing a list is using slicing. Following statement reverses and returns the resulting list.
reversed_list = mylist[::-1]
Examples
1. Reverse a given list of numbers using reverse()
In the following example, we have a List of numbers. We will reverse the list using reverse() method.
Python Program
# A list of numbers
mylist = [21, 5, 8, 52, 21, 87, 52]
# Reverse the list
mylist.reverse()
# Print the list
print(mylist)
Output
[52, 87, 21, 52, 8, 5, 21]
Explanation
- The list starts as [21, 5, 8, 52, 21, 87, 52].
- The
reverse()
method is called, which reverses the list in place. - The original list is updated to [52, 87, 21, 52, 8, 5, 21].
2. Reverse the list using slicing
In this example, we will use slicing technique and reverse the given list.
Python Program
# List of numbers
mylist = [21, 5, 8, 52, 21, 87, 52]
# Reverse list using slicing
mylist = mylist[::-1]
# Print the list
print(mylist)
Output
[52, 87, 21, 52, 8, 5, 21]
Explanation
- The list starts as [21, 5, 8, 52, 21, 87, 52].
- Slicing with
[::-1]
creates a new reversed list. - The original list remains unchanged, but the variable is reassigned to the reversed list.
3. Reverse a given list of strings
In this example, we reverse a list of strings.
Python Program
# List of strings
mylist = ['list', 'dict', 'set']
# Reverse the list
mylist.reverse()
# Print the list
print(mylist)
Output
['set', 'dict', 'list']
Explanation
- The list starts as ['list', 'dict', 'set'].
- The
reverse()
method reverses the list in place. - The modified list becomes ['set', 'dict', 'list'].
4. Reverse a mixed data type list
In this example, we reverse a list containing different data types.
Python Program
# List with mixed data types
mylist = [1, 'hello', 3.14, True]
# Reverse the list
mylist.reverse()
# Print the list
print(mylist)
Output
[True, 3.14, 'hello', 1]
Explanation
- The list starts as [1, 'hello', 3.14, True].
- The
reverse()
method reverses the list in place. - The modified list becomes [True, 3.14, 'hello', 1].
5. Reverse list using recursion
This example demonstrates reversing a list using recursion.
Python Program
# Recursive function to reverse a list
def reverse_list(lst):
if len(lst) == 0:
return []
return [lst[-1]] + reverse_list(lst[:-1])
# List to reverse
mylist = [21, 5, 8, 52]
# Call the recursive function
reversed_list = reverse_list(mylist)
# Print the reversed list
print(reversed_list)
Output
[52, 8, 5, 21]
Explanation
- The function
reverse_list
is called with the input list. - It appends the last element of the list to the result of the function called on the rest of the list.
- The final reversed list is returned as [52, 8, 5, 21].
Summary
In this tutorial of Python Examples, we learned how to reverse a Python List, with the help of well-detailed example programs.