You are currently viewing 8 Different Ways To Reverse A Python List

8 Different Ways To Reverse A Python List

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.

Output

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.

Output

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.

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

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.

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

If you are wondering how it happened, then look at the example below

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.

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.

The above code will step over the elements from the original list in reverse order.

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.

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.

Using range function

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.

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.

Output

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✌✌