How do I Reverse a List backwards? | Python Tutorial

In this tutorial, we will discuss different ways, How do I reverse a list backward in Pyron. There are two ways to do so where we can get the list in reverse order one is using the function reverse and the other is using slicing as we discussed earlier.

What Is List In Python?

The list is a mutable collection of values that can store different types of data at their indexes for the exam it can store integers, Gloat, String, Caracter, and even Decimal values in a single list.Traverse the list in Reverse Order in Python Traversing Of List

We can traverse the list in different ways where we can get the elements of list in different ways and even in any of the given patterns. Moreover, to get the elements of the list we need to traverse the list even by slicing which is again a very huge topic

Traversing In Reverse Order

Traversing a list in reverse order means getting the elements of list from another end instant of getting elements from the front the elements being displayed from the other ends. we can display the elements in reverse order in different ways  that either using the reverse function or using slicing  So let us discuss all the possible ways to do so

Reverse Using A Function

It is a way to display the list in reverse order simply by using an in build function of python so we can do this like.

>>> xs = [0, 10, 20, 40]
>>> list(reversed(xs))

Here we have a list of [ 0, 10, 20, 40] which is passed to in build function and is used simply for returning the elements of a list in reverse order.

So it will result in [ 40, 20, 10, 0]. It is only used for displaying the list in reverse order it will not allow us to make any changes while displaying the values in reverse order So to overcome this situation we can with another example that was slicing.

So let’s see how we can reverse the elements of the list  using Slicing

Reverse Using Slicing

As we had already discussed how we can traverse the elements of a list in reverse order using the in-build function reverse. So now we will discuss the method of slicing to do so.

As we know that slicing is used for displaying the elements of a list in different ways or we can say if we want to get the elements in some pattern then we use slicing the reverse is also a type of pattern where the last element is displayed first and in same order first at last.

So let’s take an example to do so;

>>> xs = [0, 10, 20, 40]
>>> xs[::-1]

As here we can say how we have to give -1 at the place of hopping( Leaving the elements or indexes) to display the list so it is going to give an answer as the reverse of the list we have provided.

So the result of given lines of code will also be [ 40, 20, 10, 0]. the same as the result of the inbuild function reverse.

So we can learn more about traversing the list in reverse order at: https://stackoverflow.com/questions/3940128/how-do-i-reverse-a-list-or-loop-over-it-backwards

And also learn from livingSlicing in python.

Leave a Comment

%d bloggers like this: