Using a for loop on any Python object is very easy as compared to other programming languages like C++ and Java. But have you ever tried using a for loop backwards? It’s as easy as a general for loop that you use while iterating over any Python object. So if you want to learn how to use a backward for loop, then this article is for you. In this article, I will take you through a tutorial on how to use a backward for loop using Python.
Backward For Loop using Python
For loop is used to iterate over any Python object. If you want to return the index of any values, then you should always prefer a for loop. Below is how you can use a for loop over a list:
list_ = ["Aman", "Kharwal", "Akanksha", "Hritika", "Shiwangi"] for i in list_: print(i)
Aman Kharwal Akanksha Hritika Shiwangi
Now coming back to using a for loop backwards, Python has an inbuilt function known as reversed() that can be used to reverse the order of a Python object while using a for loop. So below is how you can use a backward for loop using Python:
for i in reversed(list_): print(i)
Shiwangi Hritika Akanksha Kharwal Aman
The inbuilt function in Python known as reversed() can be used to use a for loop backwards. So this is how you can easily use a for loop backwards in the Python programming language. I hope you liked this article on a tutorial on how to use a backward for loop using the Python programming language. Feel free to ask your valuable questions in the comments section below.