How to Loop Backwards in Python: Easy Methods

How to Loop Backwards in Python Easy Methods

Do you want to loop backward in python and print on the screen? If yes then in this post you will know how to loop backward in python using different functions. You will know how to use the reversed() function to loop backward. As you already know that for loop allows you to iterate in the forward direction. But if you use the reversed() function with the range() function then you will easily loop backwards in Python.

Methods to Loop Backwards in Python

Let’s know all the methods that can help you in looping backward in python.

Method 1: Loop backward using the reversed() Function

As you already know if you will use the for loop with the range(1,10) will loop and print elements from 1 to 9. But if you want to loop in the backward direction then enclose the range(1,10) function inside the reversed() function. The below lines of code will print the values inside the range in the reverse direction.

for i in reversed(range(1,10)):
    print(i)

Output

Using reversed() function to loop backward direction
Using reversed() function to loop backward direction

Method 2: Using the range() function

You can also loop backward using the range() function. Here you have to tell the function to iterate in the opposite direction by passing the arguments as last, first, and -1 respectively.

Use the below lines of code to achieve that.

for i in range(9,0,-1):
    print(i)

Output

Using range() function to loop backward direction
Using range() function to loop backward direction

Method 3: Slice the list for the backward loop

If you have a list and want to print each element in reverse direction then you can use the list slicing. Here you will pass the -1::-1 inside the square bracket and print the elements from the last. Here -1 denotes the index for the end element and 0 for the first element.

You will get the elements printed in the reverse direction when you will run the below lines of code.

list=[10, 20, 50, 30, 40]
for i in list[-1::-1]:
    print(i)

Output

reversing the list using the square bracket
reversing the list using the square bracket

Conclusion

Sometimes you may have to loop backward in python. For example, to access the last element of the list you will use the -1:0 to get the last. The above methods use the reversed() and range() functions to loop backward in the reverse direction.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner