The reverse list python function allows the programmer to reverse the sorting order of the elements stored in the lists. Python provides a built-in function called as the reverse() function which reverses the order of the data elements in place.
reverse list Python
Python has a built-in reverse() function in order to reverse the contents stored in the list. The name of this function is Python list reverse() which is used only for the list data type. Python reverse list method does not create a new list, but in turn, it directly modifies the existing list to it in the reverse order. Let us look at the Python reverse() function, its syntax, and few examples to understand the concept in more depth.
Python reverse list Function
The python reverse() function works on the basic functionality of swapping the last element with the first element, the second last element with the second element, and so on which are stored in the lists, tuples, dicts, etc. If the data type is empty, then the reverse function will return an empty data type.
The reverse() function does not take any parameters.
Also, the reverse() function does not return any value. It simply updates the contents of the existing list/tuple/dict.
Python reverse() function can be used in 3 different ways. It provides the user with 3 various ways of using this built-in function.
Method 1 – How to reverse a list in python
# Reversing a list car_brands = ['BMW', 'Audi', 'Mercedes', 'Jaguar'] print('Original list: ', car_brands) car_brands.reverse() print('Reversed list: ', car_brands)
Original list: ['BMW', 'Audi', 'Mercedes', 'Jaguar'] Reversed list: ['Jaguar', 'Mercedes', 'Audi', 'BMW']
Note – This reverse() method does not work in the case of tuple or dict. We have a different method for reversing tuples or dict. This can be seen in the below program.
num = (1, 2, 3, 4, 5) print('Original tuple: ', num) num.reverse() print('Reversed tuple: ', num)
Original list: (1, 2, 3, 4, 5) Traceback (most recent call last): File "", line 4, in AttributeError: 'tuple' object has no attribute 'reverse'
Method 2 – python list reverse using Slicing Operator
In this method, we use the Slicing operator in order to reverse the data elements. It is a special case where slicing the list with ‘[::-1]’ returns a reversed copy of a list. This method uses up more memory space because like the previous method it does not update the existing list. Instead, it creates a copy of the list and this list requires allocation of the memory location to hold the existing elements.
my_list = ['Hi', 'how', 'are', 'you'] print('Original list: ', my_list) rev_list = my_list[::-1] print('Reversed list: ', rev_list)
Original list: ['Hi', 'how', 'are', 'you'] Reversed list: ['you', 'are', 'how', 'Hi']
Method 3– Reversing Using Reversed() Function
Using this function we can reverse the elements of lists as well as tuples and dicts. This method is a better version of the first reverse() method.
a = [1, 2, 3, 4, 5] b = ('a', 'b', 'c', 'd') a = list(reversed(a)) print(a) print(tuple(reversed(b)))
[5, 4, 3, 2, 1] ('d', 'c', 'b', 'a')
Accessing elements in Reversed Order
Elements of the list can be reversed and accessed in Python. These individual elements of a reversed list can be accessed using the 3rd Method of reverse() function.
names = ['Sam', 'John', 'Jake', 'Bella', 'Ash'] for i in reversed(names): print(i)
Ash Bella Jake John Sam
FAQs on reverse list python
Q1. How do I reverse a list in Python?
Answer. To reverse a list in Python, we have a built-in function known as reverse(). The reverse() function swaps the entire list of elements in the opposite order. For example –
names = ['Sam', 'John', 'Jake', 'Bella', 'Ash'] names.reverse() print(names)
['Ash', 'Bella', 'Jake', 'John', 'Sam']
Q2. How do you reverse in Python 3?
Answer. Python program as a whole, be it any version accepts the reverse() function for reversing the elements of the list. The below program is executed in Python 3, will produce the same output as in any Python version.
subj = ['Physics', 'Biology', 'Chemistry', 'Math'] subj.reverse() print ("Subject List: ", subj)
Subject List: ['Math', 'Chemistry', 'Biology', 'Physics']
Q3. How do you reverse a list without using the reverse() function in Python?
Answer. In order to reverse a list without using the built-in reverse() function, we use the Slicing Operator. The slicing operator is another method used for reversing the data elements. Let us understand this by looking at an example –
info = [1, 'Raj', 2, 'Sejal', 3, 'Josh'] a = info[::-1] print('Reversed List: ', a)
Reversed List: ['Josh', 3, 'Sejal', 2, 'Raj', 1]