The Wayback Machine - https://web.archive.org/web/20240928024121/https://www.geeksforgeeks.org/python-list-reverse/
Open In App

Python List reverse()

Last Updated : 09 May, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
Reverse the order of all nodes at even position in given Linked List
Given a linked list A[] of N integers, the task is to reverse the order of all integers at an even position. Examples: Input: A[] = 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULLOutput: 1 6 3 4 5 2Explanation: Nodes at even position in the given linked list are 2, 4 and 6. So, after reversing there order, the new linked list will be 1 -> 6
10 min read
Reverse a Doubly Linked List without swapping nodes
Write a program to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Approach: In the previous post, doubly linked list is being reversed by swapping prev and next pointers for all nodes, changing prev of the head (or start) and then changing the head pointer in the
10 min read
Reverse first K elements of given linked list
Given a pointer to the head node of a linked list and a number K, the task is to reverse the first K nodes of the linked list. We need to reverse the list by changing links between nodes. check also Reversal of a linked list Examples: Input : 1->2->3->4->5->6->7->8->9->10->NULL, k = 3Output : 3->2->1->4->5-
9 min read
Print Reverse a linked list using Stack
Given a linked list, print the reverse of it without modifying the list. Examples: Input : 1 2 3 4 5 6 Output : 6 5 4 3 2 1 Input : 12 23 34 45 56 67 78 Output : 78 67 56 45 34 23 12 Below are different solutions that are now allowed here as we cannot use extra space and modify the list. Recursive solution to print reverse a linked list. Requires e
9 min read
Reverse a Doubly Linked List by swapping data
Given a Doubly Linked List, we are asked to reverse the list in place without using any extra space. Examples: Input : 1 <--> 2 <--> 5 <--> 6 <--> 7 Output : 7 <--> 6 <--> 5 <--> 2 <--> 1 Input : 11 <--> 22 <--> 33 <--> 22 <--> 1 Output : 1 <--> 22 <--> 33 <--
10 min read
C Program to reverse each node value in Singly Linked List
A linked list is a linear collection of data elements, in which each node points to the next node. Unlike an array, it doesn't have upper limit and hence extremely useful. The task is to access value of each node of linked list and reverse them. Examples: Input : 56 87 12 49 35 Output : 65 78 21 94 53 Input : 128 87 12433 491 33 Output : 821 78 334
2 min read
Reverse a doubly circular linked list
The problem is to reverse the given doubly circular linked list. Examples: Input: Output: Algorithm: insertEnd(head, new_node) Declare last if head == NULL then new_node->next = new_node->prev = new_node head = new_node return last = head->prev new_node->next = head head->prev = new_node new_node->prev = last last->next = new_n
11 min read
Print Doubly Linked list in Reverse Order
Given a doubly-linked list of positive integers. The task is to print the given doubly linked list data in reverse order. Examples: Input: List = 1 <=> 2 <=> 3 <=> 4 <=> 5 Output: 5 4 3 2 1 Input: 10 <=> 20 <=> 30 <=> 40 Output: 40 30 20 10 Approach: Take a pointer to point to head of the doubly linked list
7 min read
Reverse all the word in a String represented as a Linked List
Given a Linked List which represents a sentence S such that each node represents a letter, the task is to reverse the sentence without reversing individual words. For example, for a given sentence "I love Geeks for Geeks", the Linked List representation is given as: I-> ->l->o->v->e-> ->G->e->e->k->s-> ->f-
15 min read
Reverse a doubly linked list in groups of given size | Set 2
Given a doubly-linked list containing n nodes. The problem is to reverse every group of k nodes in the list. Examples: Input: List: 10<->8<->4<->2, K=2Output: 8<->10<->2<->4 Input: List: 1<->2<->3<->4<->5<->6<->7<->8, K=3Output: 3<->2<->1<->6<->5
15+ min read
Reverse given Linked List in groups of specific given sizes
Given the linked list and an array arr[] of size N, the task is to reverse every arr[i] nodes of the list at a time (0 ≤ i < N). Note: If the number of nodes in the list is greater than the sum of array, then the remaining nodes will remain as it is. Examples: Input: head = 1->2->3->4->5->6->7->8->9, arr[] = {2, 3, 3}Outp
8 min read
Given a linked list, reverse alternate nodes and append at the end
Given a linked list, reverse alternate nodes and append them to the end of the list. Extra allowed space is O(1) Examples: Input: 1->2->3->4->5->6 Output: 1->3->5->6->4->2 Explanation: Two lists are 1->3->5 and 2->4->6, reverse the 2nd list: 6->4->2. Merge the lists Input: 12->14->16->18->
11 min read
Merge two sorted linked lists such that merged list is in reverse order
Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Examples: Input: a: 5->10->15->40 b: 2->3->20 Output: res: 40->20->15->10->5->3->2Input: a: NULL b: 2->3->20 Output: res: 20->3->2Recommended PracticeMerge 2 sorted linked
15+ min read
Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)
Given pointer to the head node of a linked list, the task is to reverse the linked list. Examples: Input : Head of following linked list 1->2->3->4->NULL Output : Linked list should be changed to, 4->3->2->1->NULL Input : Head of following linked list 1->2->3->4->5->NULL Output : Linked list should be changed
13 min read
C++ Program For Merging Two Sorted Linked Lists Such That Merged List Is In Reverse Order
Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Examples: Input: a: 5->10->15->40 b: 2->3->20 Output: res: 40->20->15->10->5->3->2 Input: a: NULL b: 2->3->20 Output: res: 20->3->2Recommended: Please solve it on "PRACTIC
4 min read
Reverse a Linked List
Given a linked list, the task is to reverse the linked list by changing the links between nodes. Examples: Input: Linked List = 1 -> 2 -> 3 -> 4 -> NULL Output: Reversed Linked List = 4 -> 3 -> 2 -> 1 -> NULL Input: Linked List = 1 -> 2 -> 3 -> 4 -> 5 -> NULL Output: Reversed Linked List = 5 -> 4 -> 3 -
15+ min read
Reverse a sublist of linked list
Given a linked list and positions m and n. We need to reverse the linked list from position m to n. Examples: Input : linkedlist : 10->20->30->40->50->60->70->NULL , m = 3 and n = 6Output : 10->20->60->50->40->30->70->NULLExplanation: Linkedlist reversed starting from the node m i.e. 30 and n i.e. 60 Input
15+ min read
Reverse alternate K nodes in a Singly Linked List
Given a linked list, The task is to reverse alternate k nodes. If the number of nodes left at the end of the list is fewer than k, reverse these remaining nodes or leave them in their original order, depending on the alternation pattern. Example: Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL, k = 2Output: 2 -> 1 -> 3 -> 4 -
15+ min read
Javascript Program For Merging Two Sorted Linked Lists Such That Merged List Is In Reverse Order
Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Examples: Input: a: 5->10->15->40 b: 2->3->20 Output: res: 40->20->15->10->5->3->2Input: a: NULL b: 2->3->20 Output: res: 20->3->2A Simple Solution is to do following. Rev
4 min read
Reverse a doubly linked list in groups of K size
Given a Doubly linked list containing n nodes. The task is to reverse every group of k nodes in the list. If the number of nodes is not a multiple of k then left-out nodes, in the end should be considered as a group and must be reversed. Examples: Input: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6 <-> NULL, k = 2Output: 2
15+ min read
Reverse a singly Linked List in groups of given size (Recursive Approach)
Given a Singly linked list containing n nodes. The task is to reverse every group of k nodes in the list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should be considered as a group and must be reversed. Example: Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL, k = 2 Output: head: 2 -> 1 ->
9 min read
Reverse a Linked List in groups of given size
Given a Singly linked list containing n nodes. The task is to reverse every group of k nodes in the list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should be considered as a group and must be reversed. Example: Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL, k = 2 Output: head: 2 -> 1 ->
3 min read
Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to la ist to perform tasks is quite common in web development. Let's discuss certain ways in which this
7 min read
Python Slicing | Reverse an array in groups of given size
Given an array, reverse every sub-array formed by consecutive k elements. Examples: Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] k = 3 Output: [3, 2, 1, 6, 5, 4, 9, 8, 7] Input: arr = [1, 2, 3, 4, 5, 6, 7, 8] k = 5 Output: [5, 4, 3, 2, 1, 8, 7, 6] Input: arr = [1, 2, 3, 4, 5, 6] k = 1 Output: [1, 2, 3, 4, 5, 6] Input: arr = [1, 2, 3, 4, 5, 6, 7, 8] k =
5 min read
Reverse bits of a positive integer number in Python
Given an positive integer and size of bits, reverse all bits of it and return the number with reversed bits.Examples: Input : n = 1, bitSize=32 Output : 2147483648 On a machine with size of bit as 32. Reverse of 0....001 is 100....0. Input : n = 2147483648, bitSize=32 Output : 1 We can solve this problem quickly in Python. Approach is very simple,
4 min read
Reverse Words in a Given String in Python
We are given a string and we need to reverse words of a given string Examples: Input : str =" geeks quiz practice code" Output : str = code practice quiz geeks Input : str = "my name is laxmi" output : str= laxmi is name my Reverse the words in the given string program C/C++ Code # Python code # To reverse words in a given string # input string str
6 min read