Nested loops

A loop can contain one or more other loops: you can create a loop inside a loop.
This principle is known as nested loops. Nested loops go over two or more loops.

Programmers typically nest 2 or 3 levels deep. Anything higher than that is just confusing.

Related course: Complete Python Bootcamp: Go from zero to hero in Python


Lets do a simple example. We create two lists:

If we have a list of persons who like to eat at restaurants, can we make every one of them eat a certain restaurant?

This goes over both loops:

nested loop output

Study drill

Try the exercises below

After completing these continue with the next exercise.