for Loops in Python

Loops are used when you need to repeat a block of code a certain number of times or apply the same logic over each item in a collection.

There are two types of loops: for and while.

In this article, I will focus on for loops.

To learn about while loops, check out this article on While Loops in Python.

Basic Syntax

The basic syntax of a for loop is as below.

for item in collection:
    statement

Loop over a list

To loop over a list or any other collection, just proceed as the example below.

cars = ['BMW', 'Ferrari', 'McLaren']
for car in cars:
    print(car)
#output:
#BMW
#Ferrari
#McLaren

The list of cars contains three items, the for loop will iterate over the list and store each item in the car variable, and then execute a statement, in this case, print(car) to print each car in the console.

range() function

The range function is widely used in for loops because it gives you a simple way to list numbers.

This code will loop through the numbers 0 to 5 and print each of them.

for number in range(5):
    print(number)
#output:
#0
#1
#2
#3
#4

In contrast, without the range() function, we would do something like this.

numbers = [0, 1, 2, 3, 4]
for number in numbers:
    print(number)
#output:
#0
#1
#2
#3
#4

You can also define a start and stop using range.

for number in range(5, 10):
    print(number)
#output:
#5
#6
#7
#8
#9

Finally, it is also possible to set a step.

for number in range(10, 20, 2):
    print(number)
#output:
#10
#12
#14
#16
#18

else block

You can use the else block, similar to what is done in Conditionals in Python.

When the items in the list are over, the else block will be called.

cars = ['BMW', 'Ferrari', 'McLaren']
for car in cars:
    print(car)
else:
    print('No cars left!')
#output:
#BMW
#Ferrari
#McLaren
#No cars left!

How to break out of a for loop in Python?

Simply use the break keyword, and the loop will stop its execution.

cars = ['BMW', 'Ferrari', 'McLaren']
for car in cars:
    print(car)
    if car == 'Ferrari':
        break
#output:
#BMW
#Ferrari

The loop will iterate the list and print each car.

In this case, after the loop reaches ‘Ferrari’, the break is called and ‘McLaren’ won’t be printed.

How to skip an item in a for loop?

The continue will do that for you.

I had to invert the order of the if statement and the continue to show how it works properly.

Notice that I always check if ‘Ferrari’ is the current item, if it is, ‘Ferrari’ won’t be printed, and the continue will skip to the next item ‘McLaren’.

cars = ['BMW', 'Ferrari', 'McLaren']
for car in cars:
    if car == 'Ferrari':
        continue
    print(car)
#output:
#BMW
#McLaren

Loop over a Loop: Nested Loops

Sometimes you have more complex collections, like a list of lists.

To iterate over these lists, you need nested for loops.

In this case, I have three lists, one of BMW models, another on Ferrari models, and finally one with McLaren models.

The first loop iterates over each brand’s list, and the second will iterate over the models of each brand.

car_models = [ ['BMW I8', 'BMW X3', 'BMW X1'], 
['Ferrari 812', 'Ferrari F8', 'Ferrari GTC4'], 
['McLaren 570S', 'McLaren 570GT', 'McLaren 720S']]

for brand in car_models:
    for model in brand:
        print(model)
#output:
#BMW I8
#BMW X3
#BMW X1
#Ferrari 812
#Ferrari F8
#Ferrari GTC4
#McLaren 570S
#McLaren 570GT
#McLaren 720S

Now that you’ve learned how to use for loops, I highly recommend this post on List Comprehensions in Python: prettier for loops, another very useful tool to work with lists.