reverse() is an inbuilt method in Python programming language that reverses objects of list in place.
Syntax:
list_name.reverse()
Parameters:
There are no parameters
Returns:
The reverse() method does not return any value but reverse the given object from the list.
Code#1:
# 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:
When anything other than list is used in place of list, then it returns an AttributeError
Code#2:
# 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'
Practical Application:
Given a list of numbers, check if the list is palindrome.
Note: palindrome-sequence that reads the same backwards as forwards
Code#3:
# Python3 program for the # practical application of reverse() list1 = [1, 2, 3, 2, 1] # store a copy of list list2 = list1.copy() # reverse the list list2.reverse() # compare reversed and original list if list1 == list2: print("Palindrome") else: print("Not Palindrome") |
Output:
Palindrome
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
- Python | Reverse sequence of strictly increasing integers in a list
- Python | Reverse each tuple in a list of tuples
- Python | Reverse sign of each element in given list
- Python | Reverse Order Sort in String List
- Python | Reverse All Strings in String List
- Python - Reverse Row sort in Lists of List
- Python - Reverse Range in String List
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Reverse alternate K nodes in a Singly Linked List
- Given a linked list, reverse alternate nodes and append at the end
- Merge two sorted linked lists such that merged list is in reverse order
- Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)
- Reverse a Linked List according to its Size
- Reverse a sublist of linked list
- Reverse a Doubly Linked List | Set-2
- Reverse a Doubly Linked List | Set 4 (Swapping Data)
- Reverse a doubly linked list in groups of given size
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

