car side mirror view of road and lampposts during golden hour
Photo by Hunter James on Unsplash

Looping through a list in Python is quite straightforward. 

Let us mention two ways that you can do it.

You can do it using indexes and the length of the list, like the following:

We can also do that by simply iterating through the list:

Now if we want to iterate through a reversed list, we can do that in a few ways, but here we are only going to mention 2 methods.

Let’s see the first one. This is similar to the index-based iteration, but here we just need to be a little bit careful with the indexes.

Since we need to go from a larger number towards a smaller one, we are going to add a negative number to each number that we have reached in the range:

Now there is a quicker way that is based on reversing the list and then iterating through its elements. Here we rely on the method that reverses the elements in the list and then we simply have to iterate through it:

We can also do that by using the method reverse() so it can seem less cryptic:


That’s basically it.

I hope you find this useful and entertaining to give it a try.