In this tutorial, you’ll learn how to decrement a for loop in Python. A for loop is used to iterate over an item, such as a Python list. In particular, they’re used to loop over an item a certain number of times. By default, Python for loops will increment. But this tutorial, will teach you different ways to accomplish the opposite. You’ll learn how to decrement a Python for loop using the
range function, the Python
reverse function, a Python
for loop, and a Python
What does it mean to decrement a Python for loop? Generally, we increment a for loop, meaning we loop over our sequence in increasing order. For example, looping over the list containing
[0,1,2,3] would start at 0 and increment by 1, through to the end of the list. By decrementing, we loop over our list from the last value and decrease from there, to the first value. This would result in our list being looped over in reverse order, resulting in
[3, 2, 1, 0].
The Quick Answer: Use
# Looping over a list in reverse order names = ['Nik', 'Kate', 'Evan', 'Kyra', 'Jane'] for name in reversed(names): print(name) # Returns: # Jane # Kyra # Evan # Kate # Nik
Use the Range Function to Decrement a For Loop in Python
range() function is an incredibly versatile function, which allows us to generate a sequence of numbers. Let’s take a look at what the function looks like:
# The Python range() Function Explained range( start = # The starting number , stop = # The end number , step = # The number by which to increment )
Let’s break these parameters down a little further:
|Parameter||Description||Required / Optional||Default Value|
|The integer that specifies the starting position.||Optional||0|
|The integer that specifies the ending position.||Required||n/a|
|How to increment (or decrement) the values.||Optional||1|
In order to decrement a for loop in Python, we can make creative use of this function.
Let’s load a Python list and see how we can use a for loop to print it in reverse order:
# Loading a Sample Python List names = ['Nik', 'Kate', 'Evan', 'Kyra', 'Jane']
We can loop over this list in reverse order by making the starting position the last index and decrementing by 1. Let’s see how we can do this:
# Looping Over a List in Reverse Order last_index = len(names) - 1 end_position = -1 increment = -1 for i in range(last_index, end_position, increment): print(names[i]) # Returns: # Jane # Kyra # Evan # Kate # Nik
Let’s break down what we did here:
- We loaded a variable,
last_indexand gave it the value of the length of the list minus 1. The reason for this is that because Python uses 0-based indexing, we would encounter an
IndexErrorif we used the full length.
- We set the
end_positionto -1 (rather than 0) since the range function goes to the end position, but doesn’t include it. Setting it to 0 would omit the first value.
- We set our
incrementto -1 to instruct Python to decrement by 1.
We can simplify this by simply including all the instructions inline, as shown below:
for i in range(len(names) - 1, -1, -1): print(names[i])
In the next section, you’ll learn how to use the
reversed() function to decrement a for loop in Python.
Use the Reversed Function to Decrement a For Loop in Python
reversed() function takes an iterable object, such as a list, and returns a reversed object. This object represents our iterable object in, well, reversed order.
Let’s see how we can loop over a Python list in reverse order using the
# Looping Over a List in Reverse Order using reversed() for name in reversed(names): print(name) # Returns: # Jane # Kyra # Evan # Kate # Nik
We can see that this approach is a lot cleaner to write and to read! It is also a little bit less flexible than using the
range() function, which allows us to, for example, iterate over a list in reverse order and decrement by a number other than 1.
reversed() function returns a list-like object. We can see what this looks like printing out the object:
# Checking the reversed() object print(reversed(names)) # Returns: <list_reverseiterator object at 0x7f86981f26a0>
As seen, we can iterate over the reversed object. If we wanted to see its contents all at once, we can convert it back to a list, by using the
# Reversing a list with reverse() print(list(reversed(names))) # Returns: ['Jane', 'Kyra', 'Evan', 'Kate', 'Nik']
In the next section, you’ll learn how to decrement a while loop in Python using augmented assignment operators.
Decrement a While Loop in Python
We can also use a Python while loop to decrement our iteration. A Python while loop runs indefinitely while a condition remains
True. We can use our while loop to loop over a list in reverse order and tell Python to stop iterating when the start of the list has been reached.
Let’s see what this looks like and then explore how it works:
# Looping Over a List in Reverse Order using a while loop i = len(names) - 1 while i >= 0: print(names[i]) i -= 1 # Returns: # Jane # Kyra # Evan # Kate # Nik
The way that this works is by first setting an integer value to the last index of our list. We can get this position by subtracting 1 from the length of the list. We then loop over our list while the value of our integer is greater than or equal to 0.
After each iteration, we decrement the value of our integer by 1, using the augmented assignment operator.
In this tutorial, you learned how to use Python to decrement a for loop and a while loop. You learned how to iterate over an object in reverse order, using both indexing with the negative step counter as well as with the
To learn more about the method covered off in this tutorial, check out the official documentation here: Range function, for loop, and while loop official documentation.
To learn more about similar topics, check out my tutorial listed below: