Open In App

Python List reverse()

Last Updated : 09 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python List reverse() is an inbuilt method in the Python programming language that reverses objects of the List in place i.e. it doesn’t use any extra space but it just modifies the original list.

Python List reverse() Syntax

Syntax: list_name.reverse()

Parameters: There are no parameters.

Returns: The reverse() method does not return any value but reverses the given object from the list.

List reverse() in Python Example

Reverse a list using list reverse()

Here we are reversing the list using the list reverse() function in Python.

Python3




# Python3 program to demonstrate the
# use of reverse method
  
# a list of numbers
list1 = [1, 2, 3, 4, 1, 2, 6]
list1.reverse()
print(list1)
 
# a list of characters
list2 = ['a', 'b', 'c', 'd', 'a', 'a']
list2.reverse()
print(list2)


Output: 

[6, 2, 1, 4, 3, 2, 1]
['a', 'a', 'd', 'c', 'b', 'a']

Error in reverse() Method

When anything other than list is used in place of list, then it returns an AttributeError.

Python3




# Python3 program to demonstrate the
# error in reverse() method
  
# error when string is used in place of list
string = "abgedge"
string.reverse()
print(string)


Output:

Traceback (most recent call last):
 File "/home/b3cf360e62d8812babb5549c3a4d3d30.py", line 5, in 
   string.reverse() 
AttributeError: 'str' object has no attribute 'reverse' 

Reverse a List using the Slicing Operator

In this example, the [::-1] slicing operator creates a new list which is the reverse of the my_list.

Python3




my_list = [1, 2, 3, 4, 5]
reversed_list = my_list[::-1]
print(reversed_list)


Output :

[5, 4, 3, 2, 1]

Reversing a sublist using Slicing

In this example, we are reversing a sublist from index 1 to 3 using [::-1] operator.

Python3




my_list = [1, 2, 3, 4, 5]
print('Original list:', my_list)
my_list[1:4] = my_list[1:4][::-1]
print('Reversed sublist:', my_list)


Output :

Original list: [1, 2, 3, 4, 5]
Reversed sublist: [1, 4, 3, 2, 5]

Accessing Elements in Reversed Order

In this example, we are traversing the list in the reverse order.

Python3




my_list = [1, 2, 3, 4, 5]
for element in reversed(my_list):
    print(element)


Output :

5
4
3
2
1

Reversing a list of mixed DataTypes

In this example, we are reversing the list of mixed data types with the reverse() function.

Python3




my_list = [1, 'apple', 2.5, True]
print('Original list:', my_list)
my_list.reverse()
print('Reversed list:', my_list)


Output :

Original list: [1, 'apple', 2.5, True]
Reversed list: [True, 2.5, 'apple', 1]

Practical Application

Given a list of numbers, check if the list is a palindrome

Python3




# Python3 program for the
# practical application of reverse()
list_arr = [1, 2, 3, 2, 1]
list_string = list("naman")
 
# store a copy of list
list2 = list_arr.copy()
list3 = list_string.copy() 
 
# reverse the list
list2.reverse()
list3.reverse()
 
# compare reversed and original list
if list_arr == list2:
    print(list_arr, ": Palindrome")
else:
    print(list_arr, ": Not Palindrome")
 
# compare reversed and original list
if list_string == list3:
    print(list_string, ": Palindrome")
else:
    print(list_string, ": Not Palindrome")


Output

[1, 2, 3, 2, 1] : Palindrome
['n', 'a', 'm', 'a', 'n'] : Palindrome

Note: Palindrome-sequence that reads the same backward as forwards.



Previous Article
Next Article

Similar Reads

Count of unique pairs (i, j) in an array such that sum of A[i] and reverse of A[j] is equal to sum of reverse of A[i] and A[j]
Given an array arr[] consisting of N positive integers, the task is to find the count of unique pairs (i, j) such that the sum of arr[i] and the reverse(arr[j]) is the same as the sum of reverse(arr[i]) and arr[j]. Examples: Input: arr[] = {2, 15, 11, 7}Output: 3Explanation:The pairs are (0, 2), (0, 3) and (2, 3). (0, 2): arr[0] + reverse(arr[2]) (
7 min read
XOR Linked List - Reverse a Linked List in groups of given size
Given a XOR linked list and an integer K, the task is to reverse every K nodes in the given XOR linked list. Examples: Input: XLL = 7< – > 6 < – > 8 < – > 11 < – > 3, K = 3 Output: 8 < – > 6 < – > 7 < – > 3 < – > 11 Explanation: Reversing first K(= 3) nodes modifies the Linked List to 8 < – > 6
13 min read
XOR linked list: Reverse last K nodes of a Linked List
Given a XOR Linked List and a positive integer K, the task is to reverse the last K nodes in the given XOR linked list. Examples: Input: LL: 7 <–> 6 <–> 8 <–> 11 <–> 3 <–> 1, K = 3Output: 7<–>6<–>8<–>1<–>3<–>11 Input: LL: 7 <–> 6 <–> 8 <–> 11 <–> 3 <–> 1 <–
14 min read
Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas provide a method to split string around a passed separator or delimiter. After that, the string can be stored as a list in a seri
3 min read
Python | Reverse sequence of strictly increasing integers in a list
Given a list of integers, write a Python program to reverse the order of consecutively incrementing chunk in given list. Examples: Input : [0, 1, 9, 8, 7, 5, 3, 14] Output : [9, 1, 0, 8, 7, 5, 14, 3] Explanation: There are two chunks of strictly increasing elements (0, 1, 9) and (3, 14). Input : [-5, -3, 0, 1, 3, 5, -2, -12] Output : [5, 3, 1, 0, -
3 min read
Python | Reverse each tuple in a list of tuples
Given a list of tuples, write a Python program to reverse each tuple in the given list of tuples. Examples: Input : [(1, 2), (3, 4, 5), (6, 7, 8, 9)] Output : [(2, 1), (5, 4, 3), (9, 8, 7, 6)] Input : [('a', 'b'), ('x', 'y'), ('m', 'n')] Output : [('b', 'a'), ('y', 'x'), ('n', 'm')] Method #1 : Negative-step slicing We can use standard negative-ste
5 min read
Python | Reverse sign of each element in given list
Given a list of integers, write a Python program to reverse the sign of each element in given list. Examples: Input : [-1, 2, 3, -4, 5, -6, -7] Output : [1, -2, -3, 4, -5, 6, 7] Input : [-5, 9, -23, -2, 7] Output : [5, -9, 23, 2, -7] Methods #1: List comprehension C/C++ Code # Python3 program to Convert positive # list integers to negative and vice
3 min read
Python | Reverse Order Sort in String List
Sometimes, while working with Python, we can have a problem in which we need to perform the reverse sort operation in all the Strings that are present in a list. This problem can occur in general programming and web development. Let’s discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + sorted() + join()
3 min read
Python | Reverse All Strings in String List
Given a list, we always come across situations in which we require to apply certain function to each element in a list. This can be easily done by applying a loop and performing an operation to each element. But having shorthands to solve this problem is always beneficial and helps to focus more on important aspects of problem. Let’s discuss certai
5 min read
Python - Reverse Row sort in Lists of List
Sometimes, while working with data, we can have a problem in which we need to perform the sorting of rows of the matrix in descending order. This kind of problem has its application in the web development and Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + sort() + reverse This problem ca
6 min read