Back to articles list Articles
6 minutes read

How to Decrement a Python for Loop

Do you know how to decrement in Python? It’s easy! You can do it with a simple for loop, and I’ll show you how.

Unlike other programming languages (such as C++) Python has no decrement operator (i.e. similar to the -- in C++). In Python, we state the beginning and the end of the iteration with the number of steps in between.

If you need a refresher on the Python for loop, read Kateryna's article on writing for loops in Python, then come back here. And if you’re taking your first steps in Python – or you just want to practice what you already know – we have a set of 5 interactive courses covering Python basic commands, data structures, and built-in algorithms in 138 coding challenges.

Okay, on to the for loop!

Incrementing a Python for Loop

You might have already learned this common way of implementing a for loop in Python:

 >>> for i in range(10):
... print(i)
0
1
2
3
4
5
6
7
8
9

We just incremented successfully! But how do we decrement? In other words, how can we print 9, 8, 7, 6, 5, 4, 3, 2, 1, 0?

If you do not know, maybe it is because you are not familiar with the complete syntax of the range() method. As per the Python documentation, its syntax is as follows:

range(start, end, step):  

If we only specify one value, by default it is the end parameter. The start and step parameters are optional; they default to 0 and 1, respectively. The above syntax is equivalent to writing:

 >>> for i in range(0, 10, 1):
... print(i)
0
1
2
3
4
5
6
7
8
9

Next, let’s explore how to use this knowledge to decrement a Python for loop.

Decrementing a Python for Loop with range()

The previous example provides some insight on how to decrement in a Python for loop using range(). We can apply it in a straightforward manner by setting the start and the end parameters so that end < start and there’s a negative step value. Let's try!

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

9
8
7
6
5
4
3
2
1
0

And here we go! We have exactly the same loop as above, but in reverse order. The start parameter is set to 9 because that is the first value we want to obtain. The end is set to -1 because the end value is always excluded; here, the last value we want to print is 0. Finally, step=-1 because it’s the difference between each number we would like to get and the previous one (e.g., 8 - 9 = -1, 2 - 3 = -1, etc.)

We can also set the step parameter to some other value (like -3) and the for loop will decrement by 3 instead of 1. Let's modify the previous example and output the results:

>>> for i in range(9, -1, -3):
... print(i)
...
9
6
3
0

We can also define a list of elements and display them in reverse order based on the list length. If you don’t remember lists and other Python data structures, I encourage you to read more about the difference between lists, tuples, and dictionaries before you continue.

Below, we see a list in reverse order:

>>> a = ["a", "b", "c", "d", "e", "f"]
>>> start = len(a) - 1
>>> end = -1
>>> step = -1
>>> for i in range(start, end, step):
... print(a[i])
...
f
e
d
c
b
a

It is important to define the start parameter as len(a) - 1 because in Python (and computer sciences in general) we start counting from 0. (However, it’s good to note that the R and MATLAB programming languages start from 1.)

In list a, we want to start decrementing from "f". While "f" is effectively the sixth element of the above list, its index is 5. For example, if I want to output "f" from the list above, I need to input a[5]. Similarly, you can also return "f" by typing a[len(a) - 1]. That’s why this index is the start.

Next, we need to set the end parameter to -1 because we want to stop at the first element, which has the index value of 0. If you find this confusing, you can read more about arrays and lists in Python.

Another way to decrement is to use the Python reversed() method.

Decrementing a Python for Loop with reverse() and reversed()

The Python reversed() function will reverse a list without modifying it. Therefore, if we want to decrement a Python for loop, we can wrap a list with the reversed() function. Let's try with a simple list of numbers:

>>> a = [1, 5, 8]
>>> for i in reversed(a):
... print(i)
...
8
5
1

And our list is printed in reversed order. However, it is essential to note that the reversed() function does not modify the list. In other words, if we print the variable a, we get the same initial list.

>>> print(a)
[1, 5, 8]

On the other hand, if we use the reverse() method, we will modify the list. We won’t need to use the reversed() function any more in the for loop, but the list will stay reversed. So if you want to keep the original list unchanged, you should use reversed().

>>> a = [1, 3, 8]
>>> a.reverse()
>>> for i in a:
... print(i)
...
8
3
1

Now the variable a is [8, 3, 1] (it’s reversed). Of course, you can always reverse it again.

There are also other ways to decrement in Python – e.g. using a while loop or list properties – but I won’t cover them here. If you are interested in Python data structures (and you should be), I again encourage you to read this article on arrays and lists in Python, where you will also learn about NumPy, one of the best Python libraries for working with arrays.

Practice Decrementing Python for Loops!

This article explored how to decrement in a for loop using Python’s built-in methods. I encourage you to play with this article’s examples and modify them. If you want to go one step further, you can also try to rerun them with list comprehension and see if you can retrieve the results.

Last but not least, this article would not be complete without Luke's article on how to end loops in Python.

If you feel like you need more practice, we offer 10 hours of interactive Python exercises to help you build your coding confidence. Or check out our curated list of free Python courses and browse LearnPython.com to keep learning about Python.