Python | Reversing a List
Python provides us with various ways of reversing a list. We will go through few of the many techniques on how a list in python can be reversed.
Examples:
Input : list = [10, 11, 12, 13, 14, 15] Output : [15, 14, 13, 12, 11, 10] Input : list = [4, 5, 6, 7, 8, 9] Output : [9, 8, 7, 6, 5, 4]
Method 1: Using the reversed() built-in function.
In this method, we neither reverse a list in-place(modify the original list), nor we create any copy of the list. Instead, we get a reverse iterator which we use to cycle through the list.
# Reversing a list using reversed() def Reverse(lst): return [ele for ele in reversed(lst)] # Driver Code lst = [10, 11, 12, 13, 14, 15] print(Reverse(lst)) |
Output:
[15, 14, 13, 12, 11, 10]
Method 2: Using the reverse() built-in function.
Using the reverse() method we can reverse the contents of the list object in-place i.e., we don’t need to create a new list instead we just copy the existing elements to the original list in reverse order. This method directly modifies the original list.
# Reversing a list using reverse() def Reverse(lst): lst.reverse() return lst lst = [10, 11, 12, 13, 14, 15] print(Reverse(lst)) |
Output:
[15, 14, 13, 12, 11, 10]
Method 3: Using the slicing technique.
In this technique, a copy of the list is made and the list is not sorted in-place. Creating a copy requires more space to hold all of the existing elements. This exhausts more memory.
# Reversing a list using slicing technique def Reverse(lst): new_lst = lst[::-1] return new_lst lst = [10, 11, 12, 13, 14, 15] print(Reverse(lst)) |
Output:
[15, 14, 13, 12, 11, 10]
To get a better understanding on the slicing technique refer Slicing Techniques in Python.
Recommended Posts:
- Python | Reversing a Tuple
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Sorting list of lists with similar list elements
- Python | Add list elements with a multi-list based on index
- Python | Pair and combine nested list to tuple list
- Python | Find maximum length sub-list in a nested list
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Merge List with common elements in a List of Lists
- Python | Merge list of tuple into list by joining the strings
- Python | Replace elements in second list with index of same element in first list
- Python | Filter list of strings based on the substring list
- Python | Convert 1D list to 2D list of variable length
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.



