Reverse list in Python
In this post, you will learn how to reverse a list using the Python programming language.
A list is a sequence of indexed and ordered values. It contains a list of any type of data object with a comma separated and enclosed within a square bracket. In the development process, we may come to the situation where we need to reverse a list. There are different ways to reverse a list in Python. Here we have mentioned most of them.
Reverse a List using the reverse function
In the given Python program, we have used the reverse() method to reverse the given list. This method doesn't return any value. It updates the existing list.
# initialize a list
stationary_items = ['Pen','Pencil','Notebook','Eraser','Whitener']
print('Original List:', stationary_items)
# List Reverse
stationary_items.reverse()
# reversed list
print('Reversed List:', stationary_items)
Output of the above code:
Original List: ['Pen', 'Pencil', 'Notebook', 'Eraser', 'Whitener']
Reversed List: ['Whitener', 'Eraser', 'Notebook', 'Pencil', 'Pen']
Reverse a list using the slicing operator
We can also use the slicing operator to reverse a list. For this, we use the "[::-1]" list slicing trick to create a reversed copy. The given example demonstrates this-
# Initializing a list
house_items = ['chair','table','bed','pillow','towel']
print('Original List:', house_items )
# Reversing a list
reversed_list = house_items [::-1]
# reversed list
print(Reversed List:', reversed_list)
Output of the above code:
Original List: ['chair', 'table', 'bed', 'pillow', 'towel']
Reversed List: ['towel', 'pillow', 'bed', 'table', 'chair']
Reverse a list using Reversed() function
The reversed() method is an in-built method of Python to create a reverse order of the list. It takes an iterable object and returns the reversed version of it.
empNames = ["Henry", "Smith", "Priska", "John"]
print("Reversed List: \n")
for x in reversed(empNames):
print(x)
Output of the above code:
Reversed List:
John
Priska
Smith
Henry
Reverse a list using slice method
The slice() function returns a slice object that is used to slice any sequence. A slice object is used to specify how to slice a sequence. Here, we have used the slice method to reverse the list.
# Reverse a Python list with slice() x_list = [21,22,23,24,25] slicer = slice(None, None, -1) reversed_list = x_list[slicer] print("Reversed List: ") print(reversed_list)Output of the above code:
Reversed List:
[25, 24, 23, 22, 21]
Reverse a List using the insert() function
Python list insert() method inserts a given element at a given index in a list. In the given Python program, we check if elements are present in the list or not. If the element is not present in the list, then put it at the 0 index using the insert() method. This process will automatically reverse the list.
# input list
lst = [23, 42, 41, 56, 39, 90]
# create empty list
rev_lst = []
# checking if elements present in the list or not
for i in lst:
# reversing the list
rev_lst.insert(0, i)
# printing result
print('Original List:', lst)
print('Reversed List:', rev_lst)
Output of the above code:
Original List: [23, 42, 41, 56, 39, 90]
Reversed List: [90, 39, 56, 41, 42, 23]
Reverse a List using list comprehension
List comprehension in Python is an easy and concise way to create a new list from an existing one. In the given program, we find the length of list and then iterate over it using range. To replace the last element with the first, we subtract the length of the original list by the index of the iterator.
# Function to reverse a list
# using the list comprehension
def rev_list(org_list):
return [org_list[len(org_list) - i] for i in range(1, len(org_list)+1)]
org_list = [6, 7, 8, 9, 10, 11]
print('Original List:',org_list)
print('Reversed List:',rev_list(org_list))
Output of the above code:
Original List: [6, 7, 8, 9, 10, 11]
Reversed List: [11, 10, 9, 8, 7, 6]
Related Articles
Convert tuple to list Python
Convert dataframe column to list
Dataframe to list Python
Convert list to set Python
Python Convert XML to CSV
Python Pandas Dataframe to CSV
Python Converting a CSV File to a MySQL Table
Convert Excel to CSV Python Pandas
Generate and download a CSV file in Django
Python write to csv file
Python Pandas Dataframe to CSV
Python Pandas CSV to Dataframe
Prettytable in Python
Python dict inside list
Convert dictionary to dataframe Python
Convert List to Dataframe Python
Python add list to list
Difference between tuple and list in Python
Convert Excel to CSV Python Pandas
Alphabetical order Python
Python | Generate QR Code using pyqrcode module