The list is the most flexible data type in Python, it represents an ordered collection of elements.Lists are mutable meaning that operations can be done to them without necessarily creating another list. One such operation is the reversing, where the list elements are rearranged in reverse order.
We can reverse a list using various approaches. Some of these operations reverses the elements in place while others leads to creation of an entirely new lists containing the elements of the original list in reverse order.
The reverse() method
List objects contains the reverse()
method which reverses the elements of the list in place, meaning that the reversing happens to the original list.
my_list.reverse()
The method takes no argument, it simply rearranges the elements of the list in reverse order.
reverse a list using reverse()
method
nums = [1, 2, 3, 4, 5]
print('before: ')
print(nums)
#call the reverse method()
nums.reverse()
print("after: ")
print(nums)
my_list = ['Python', 'C++', 'Java', 'PHP', 'Javascript']
my_list.reverse()
print(my_list)
The reversed() function
The builtin reversed() function is used to iterate through the elements of an iterable object in reverse order. We can use it with lists to create a new list with the reversed elements of the original list.
reversed_list = reversed(my_list)
reverse list elements using the reversed() function
# define a list
my_list = [1, 2, 3, 4, 5]
# reverse the list
reversed_list = list(reversed(my_list))
print(reversed_list)
# define a list
my_list = ['Java', 'C++', 'Python', 'PHP']
# reverse the list
reversed_list = list(reversed(my_list))
print(reversed_list)
Reverse a list using slicing
Slicing allows us to conveniently create a new list from an existing list by extracting a subsequence of elements from the original list. The general syntax of slicing is as shown below:
my_list[start:stop:step]
start |
Optional. The index(negative or positive) from which the slicing will begin, inclusive. Defaults to 0. |
stop |
Optional. The index(negative or postive) at which the slicing will end, exclusive. Defaults to the length of the list. |
step |
Optional. The increment value. Defaults to 1. |
All of the three arguments are optional. When all are omitted, the new string will contain all the elements of the original string.
list1 = [1, 2, 3, 4]
list2 = list1[::]
print(list2)
Consider what will happen if the value of start
is set to the strings length and step to -1.The slicing will happen in reverse, starting at the last character in the string and ending at the first character in the string.
list1 = [1, 2, 3, 4, 5]
list2 = list1[len(list1)::-1]
print(list2)
We can also omit the start
and the stop
values and the slicing will still happen in reverse when the default values are used. This is because Python also supports negative indexing.
list1 = [1, 2, 3, 4, 5]
list2 = list1[::-1]
print(list2)
# define a list
my_list = ['Java', 'C++', 'Python', 'PHP']
# reverse the list
reversed_list = my_list[::-1]
print(reversed_list)