Lists are one of the in-built data types in Python and fall into the sequence category. That means lists are used for working with the sequence of objects and we can say a sequence is a Python list when they are wrapped within ([ ]
) square brackets.
Just like other data types, lists have numerous methods and function which helps us in modifying and manipulating the elements inside the list.
Python list has a function named reverse()
which is used to reverse the elements of the list.
But in this article, you’ll see the various ways to reverse the elements specified inside the list. The list.reverse()
function is one you might know but you’ll also see other ways to perform the same operation.
Using list.reverse()
Python list.reverse()
used to reverse the elements specified inside the list. If you notice that it is list.reverse
, this function is specific to Python lists and cannot be used for other data types.
In the following example, a list is defined and stored in the my_lst
variable and then performed the reverse operation. This will reverse the order of the elements.
1 2 3 4 5 6 |
# Defining a list my_lst = ['a', 'e', 'i', 'o', 'u'] # Using reverse function my_lst.reverse() print(my_lst) |
Output
1 |
['u', 'o', 'i', 'e', 'a'] |
Note: If you try to reverse any data type other than lists, an AttributeError
will be raised stating that ‘x’ has no attribute ‘reverse’.
Using reversed()
Python reversed()
function is also used to reverse the elements inside the list. Still, instead of returning the reversed object, it returns the reversed iterator object that accesses the values of a given sequence. To access the values, you have to iterate them either by using the for
loop or the next()
function.
Consider the following example, the list object my_lst
is reversed and then accessed the values using the next()
function from the reversed iterator object new_lst
.
1 2 3 4 5 6 7 8 9 10 |
# Defining a list my_lst = ['a', 'e', 'i', 'o', 'u'] new_lst = reversed(my_lst) # Iterating over the reversed iterator obj print(next(new_lst)) print(next(new_lst)) print(next(new_lst)) print(next(new_lst)) print(next(new_lst)) |
Output
1 2 3 4 5 |
u o i e a |
One main thing is Python reversed()
isn’t like the list.reverse()
function because it can be used for any iterable sequence.
Using list comprehension
Using list comprehension for reversing the list is different from the approach you’ve seen in the above two ways.
1 2 3 4 5 6 7 8 9 |
# Defining a list my_lst = ['a', 'e', 'i', 'o', 'u'] # Taking the index value index = len(my_lst) - 1 # Using list comprehension rev_list = [my_lst[i] for i in range(index, -1, -1)] print(rev_list) |
In the above Python program, a list is defined and then the variable index
is defined that stores the list’s index value by taking the list’s length minus 1. Then created a list comprehension that accesses each index item of the list using the range function from the index
to the end of the list in reverse order.
Output
1 |
['u', 'o', 'i', 'e', 'a'] |
Using for loops
Python for
loop is a great way to iterate over a sequence. Let’s see how to reverse a list using the for
loop.
1 2 3 4 5 6 7 8 9 10 |
# Defining a list my_lst = ['a', 'e', 'i', 'o', 'u'] # Defining an empty list rev_list = list() # Iterating each element from the original list for elem in my_lst: rev_list = [elem] + rev_list print(rev_list) |
First, we defined two lists, one is our original list (my_lst
) and the other is the empty list (rev_list
) to store the reversed elements.
Then we iterated over each element from the original list and added them to our empty list named rev_list
. The output will be a reversed version of the original list.
Output
1 |
['u', 'o', 'i', 'e', 'a'] |
If you are wondering how it happened, then look at the example below
1 2 3 |
for elem in my_lst: rev_list = [elem] + rev_list print(rev_list) |
If we print rev_list
each time the elements from the original list are added then at the end we’ll get the reversed list.
1 2 3 4 5 |
['a'] ['e', 'a'] ['i', 'e', 'a'] ['o', 'i', 'e', 'a'] ['u', 'o', 'i', 'e', 'a'] |
Using reverse list indexing
The elements in Python lists have an index value that helps access the specific element. The index value starts with 0
to n-1
. List indexing can be done using the format [start : stop: step]
.
For example, [: : 1]
will return the whole list from start to end. Similarly, [: : -1]
will return the whole list but in reversed order. Let’s see an example.
1 2 3 4 5 |
# Defining a list my_lst = ['a', 'e', 'i', 'o', 'u'] new_list = my_lst[: : -1] print(new_list) |
The above code will step over the elements from the original list in reverse order.
1 |
['u', 'o', 'i', 'e', 'a'] |
Using slice method
Python has a function named slice()
that returns a slice object, which specifies how to slice a sequence. It takes 3 arguments (start, stop, step
). start
that specifies from where to start slicing, stop
that specifies where to end and step
that specifies the step of the slicing.
1 2 3 4 5 6 7 8 |
# Defining a list my_lst = ['a', 'e', 'i', 'o', 'u'] # Using the slice method reverse = slice(None, None, -1) new_list = my_lst[reverse] print(new_list) |
In the above program, created a slice object that reverses the order of the original list and is then used to reverse the original list.
1 |
['u', 'o', 'i', 'e', 'a'] |
Using range function
1 2 3 4 5 6 7 8 9 |
# Defining a list my_lst = ['a', 'e', 'i', 'o', 'u'] # Accessing the last index value index = len(my_lst)-1 # Using range function for elem in range(index, -1, -1): print(my_lst[elem], end=" ") |
In the above Python program, we are using a range()
function that is grabbing the last index value of the item and going all the way to the end in the reverse order. It is just like what we’ve done using list comprehension.
1 |
u o i e a |
Using __reversed__()
Python list has a special method called __reversed__()
that helps in reverse iteration. If you remember we saw the reversed()
function, basically it runs the __reversed__()
method in the backend to reverse the input list.
1 2 3 4 5 6 7 8 |
# Defining a list my_lst = ['a', 'e', 'i', 'o', 'u'] new_list = my_lst.__reversed__() # Iterating over the reversed object for elem in new_list: print(elem, end=" ") |
Output
1 |
u o i e a |
Conclusion
You’ve learned the various ways that you can use to reverse the Python lists. Some of the ways you might already know and some don’t.
Let’s recall the methods you’ve learned in this article to reverse the list:
- Python list
reverse()
function - Python
reversed()
function - Using list comprehension
- Using
for
loop - Reverse list indexing
- Python
slice()
method - Using
range()
function - Using
__reversed__()
method
Now, it’s your choice which method you should use but most developers go for the easy method and prefer using the list.reverse()
or reversed()
function.
🏆Other articles you might like
✅4 ways of string formatting in Python.
✅Open and read multiple files simultaneously in Python.
✅Take multiple inputs from the user in a single line in Python.
✅Ways to remove whitespace from the string in Python.
✅Python Bitwise operator and what happens behind the scene.
That’s all for now
Keep Coding✌✌