• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Backwards for Loop in Python

Backwards for Loop in Python

August 27, 2022 Leave a Comment

To loop backwards in Python with a for loop, the easiest way is to use the range() function and pass ‘-1’ for the step argument.

lst = [0, 1, 2, 3]

for i in range(3, -1, -1):
    print(lst[i])

#Output:
3
2
1
0

If you want to iterate over an iterable object, you can use the reversed() function.

lst = [0, 1, 2, 3]

for x in reversed(lst):
    print(x)

#Output:
3
2
1
0

In programming, the ability to loop and iterate over objects is one of the fundamental concepts central to all programs.

When you are looping, by default, you do it in sequential order starting from 0 and going to some number N.

If you want to loop in reverse and go backwards, how do you do it?

To loop backwards in Python with a for loop, the easiest way is to use the range() function and pass ‘-1’ for the step argument.

For example, if you wanted to loop backwards from 100 to 0, then you would do the following.

for i in range(100, -1, -1):
    do_something

If you wanted to loop backwards over a list with an index, you could do the following.

lst = [0, 1, 2, 3]

for i in range(len(lst) - 1, -1, -1):
    print(lst[i])

#Output:
3
2
1
0

Using reversed() to Loop Backwards in Python

If you have an iterable object and want to loop backwards, you can use the reversed() function.

You can pass an iterable object, such as a list, to reversed() and reversed() returns the iterable object in reversed order.

Below is a simple example showing you how you can use reversed() to loop backwards in Python.

lst = [0, 1, 2, 3]

for i in range(3, -1, -1):
    print(lst[i])

#Output:
3
2
1
0

Hopefully this article has been useful for you to learn how to loop with a for loop backwards in Python.

Other Articles You'll Also Like:

  • 1.  Find Index of Minimum in List Using Python
  • 2.  Write Float to File Using Python
  • 3.  Transpose DataFrame pandas – Using the pandas transpose Function
  • 4.  Python to_bytes() – Create Bytes Object from Integer
  • 5.  Convert String into Tuple in Python
  • 6.  Using Python turtle Module to Draw Square
  • 7.  Create List of Odd Numbers in Range with Python
  • 8.  Python Random Boolean – How to Generate Random Boolean Values
  • 9.  Python atanh – Find Hyperbolic Arctangent of Number Using math.atanh()
  • 10.  Using Python To Split String by Comma into List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy