How can I create a nested loop in Python?


#1

Question

In the context of this code challenge, how can we create a nested loop in Python?

Answer

In Python, and almost every other programming language, you can create a nested loop by placing a loop within the block of another loop.

When nesting loops, keep in mind that on each iteration of the outer loop, the inner loop will completely run.

Example

for first in first_names:
  # On each iteration of this outer loop
  # the inner loop will run to completion

  for last in last_names:
    print(first + " " + last)