Nested For Loops

A loop inside a loop, the grid it walks, and why the work multiplies rather than adds.

Overview

A loop inside a loop, the grid it walks, and why the work multiplies rather than adds.

In this guide
  1. The order things happen
  2. Where the newline goes
  3. The cost multiplies
  4. Reading a grid, row by row
  5. Breaking out of both
  6. When nesting is hiding a better idea
  7. Reading the cost without timing anything
  8. Depth, and when to stop
  9. A worked example: a formatted table
  10. itertools.product, the flat version
  11. The comprehension form, and its reading order
  12. Let the data decide the loops
  13. Measuring, rather than guessing
  14. Questions people ask about Nested For Loops
  15. Nested For Loops: recap in one screen

nested.py

nested.py Python 3
Output

                    

nested_cost.py

nested_cost.py Python 3
Output

                    

Worth knowing

The inner loop finishes entirely on every single pass of the outer one.
Two nested loops over n items run n×n bodies. Doubling n quadruples the work; it does not double it.
break leaves only the loop it is in, the inner one. The outer loop carries on.
If both loops walk the same list, you are usually comparing every pair. That is often a sign a set or dict would do it in one pass.

Nested For Loops: A Practical Guide

A loop inside a loop runs the inner one from the top on each pass of the outer one. That is easy to say and easy to under-guess: the bodies times, they don't add.

The order things happen

for row in range(3):
    for col in range(4):
        print(row, col)

The outer loop takes row = 0 and then hands control to the inner loop. The inner loop runs all four of its passes before the outer loop moves to row = 1. Twelve lines print, in row-major order. The inner variable resets each time. The outer one does not.

This is the natural shape for anything two-dimensional. A grid, a table of rows and columns, each pairing of two lists. Whenever you need to visit every cell in a spreadsheet, compare every pair of items, or generate every combination from two sets of options, nested loops are usually the first tool you reach for.

The cost grows faster than most beginners expect. A single loop over n items does n steps. Two nested loops over n items each do n times n steps. Double n and the work quadruples, not doubles. That is the core reason nested loops matter: they are the first place where performance stops being free. A loop over a thousand items finishes instantly. Nest a second loop inside and you have a million iterations. Nest a third and you are at a billion.

Understanding the execution order also matters for correctness. A common mistake is putting a print or an accumulation in the wrong loop body. If you print inside the inner loop, you get one line per cell. If you print between the inner and outer loops, you get one line per row. Placing code at the wrong indentation level is the most frequent bug in nested loops, and the fix is always the same: trace through the first two passes by hand and check where each statement fires.

Where the newline goes

A detail that catches people: print() after the inner loop, indented to the outer loop, ends the row. Indent it one level further and you get a newline after each cell instead. The indentation is the logic here, not decoration.

The cost multiplies

One loop over n items runs n bodies. Two nested loops over n items run n×n. Put them side by side instead of inside each other and you get 2n. The difference between n² and 2n is the difference between a program that scales and one that doesn't:

  • n = 100: nested runs 10,000 bodies, sequential runs 200
  • n = 400: nested runs 160,000, sequential runs 800

Doubling the input quadruples nested work. The second program on this page times both at three sizes so the shape is visible rather than asserted.

Reading a grid, row by row

The most common nested loop walks a two-dimensional structure:

grid = [[1, 2, 3], [4, 5, 6]]
for row in grid:
    for value in row:
        print(value, end=" ")
    print()

The outer loop takes a row - itself a list - and the inner loop takes the values in it. Note that the outer variable holds a whole row, not an index, which is what makes this read better than the for i in range(len(grid)) version.

When you need the coords as well, enumerate supplies them at both levels:

for r, row in enumerate(grid):
    for c, value in enumerate(row):
        print(r, c, value)

Breaking out of both

break leaves one loop. Three ways to leave two, in order of preference:

def find(grid, target):              # 1. a function, and return
    for row in grid:
        for value in row:
            if value == target:
                return value
found = None                         # 2. a flag the outer loop checks
for row in grid:
    for value in row:
        if value == target:
            found = value
            break
    if found is not None:
        break

The function is almost always the right answer: return leaves all, and the search gets a name.

When nesting is hiding a better idea

Two loops over the same collection compare each pair, and that is n squared work for a question that often has a one-pass answer.

for a in nums:                      # every pair - slow
    for b in nums:
        if a + b == target:
            ...
seen = set()                        # one pass
for n in nums:
    if target - n in seen:
        ...
    seen.add(n)

The rule of thumb: if the inner loop is searching for a thing, a set or a dict often drops it. If the two loops walk truly different things - rows and columns, users and permissions - the nesting is correct and there's nothing to drop.

Reading the cost without timing anything

You can tell the cost of a nested loop by reading it. Count how many times each loop runs and times. Two loops over the same n-item list run n times n. A loop over n holding a loop over m runs n times m. A loop over n holding a lookup in a set runs n times, because the lookup doesn't loop.

That last one is the key case. A nested loop and a loop holding a set lookup can look almost the same on the page and behave fully differently at scale, and the difference is invisible on ten items and decisive on ten thousand.

This is also why the timings on this page are run at three sizes rather than one. A single reading tells you how long a thing took. Three tell you how the time grows, which is the thing that really matters when your data gets bigger.

Depth, and when to stop

Two levels of nesting is plain. Three is worth a second look. Four is nearly always a sign that some of the structure belongs in a function, because by then no one can hold all the loop variables in their head at once, and the indentation alone pushes the real work off the right of the screen.

Pulling the inner loops into a named function costs nothing at runtime and gives each level a name that says what it's iterating over. It also gives you return, which is the cleanest way out of nested loops there's.

A worked example: a formatted table

Nested loops and the alignment that makes their output readable:

for row in range(1, 4):
    for col in range(1, 4):
        print(f"{row * col:4}", end="")
    print()
   1   2   3
   2   4   6
   3   6   9

Two details do all the work. end="" on the inner print keeps the cells on one line, and the bare print() after the inner loop — indented to the outer loop — ends the row. Indent that second print one level further and each cell gets its own line. Drop it and the whole table turns one long line. The indentation is the logic.

f"{value:4}" right-aligns each cell in four characters. This Is what stops the columns drifting once the numbers reach two digits. Counting spaces by hand works until the data changes. A width doesn't.

itertools.product, the flat version

When the nesting exists only to give each combination, product flattens it into one loop:

from itertools import product

print([f"{x}{y}" for x, y in product("ab", [1, 2])])
['a1', 'a2', 'b1', 'b2']

The order is the same as the nested loops it replaces: the last arg varies fastest, just as the innermost loop does. product takes any number of iterables, and repeat= gives the same one a few times — product(range(6), repeat=3) is each three-dice roll without three levels of indentation.

It's lazy, so it costs nothing until iterated, and it's the right tool when the combinations are the point: param sweeps, test mats, each pairing of two lists. It's the wrong tool when the inner loop depends on the outer one — iterating the cells of each row. Here The rows differ in length — because product pairs fixed sequences and can't look at the outer value to decide what the inner one should be.

itertools.combinations and permutations cover the related questions, and both avoid the nested loop plus index math that would or else be needed to compare each pair without checking any of it with itself.

The comprehension form, and its reading order

Nested loops have a comprehension equal, and the clause order is the thing people get wrong:

grid = [[1, 2], [3, 4]]

print([value for row in grid for value in row])
[1, 2, 3, 4]

The clauses appear in the same order as the equal nested for statements — outer first, inner second. That is worth stating plainly because the guess most people make is the opposite, and the wrong order raises a NameError about the inner variable. This At least fails loudly.

What is truly confusing is a *nested comprehension*, where one comprehension appears inside another's formula: [[f(x) for x in row] for row in grid] makes a list of lists rather than a flat one. Here the reading order really does invert — the outer clause is on the right and the inner work on the left.

Between the two, flattening with two for clauses is common and readable, and a comprehension inside a comprehension is where most people should stop and write the loop. The rule from elsewhere in the track applies: if it needs decoding rather than reading, the loop was the better answer.

Let the data decide the loops

A nested loop is often a description of the data's shape, and when the two disagree the loop is the thing that is wrong.

If the data is a list of rows and each row is a list of cells, two loops are correct and the outer variable should be a row rather than an index. If the data is a flat list and the nesting exists to pair items with each other, the loops are doing a search and can often be replaced. If the data is a dict of lists, the outer loop takes items() and the inner takes the list, and the key is available at both levels without any bookkeeping.

The mismatch worth watching for is a nested loop over data that is already flat. Code that iterates a list of records and then, inside, iterates the same list to find a matching record is showing a join, and a dict keyed on the join field turns it into one pass. The nesting was never about the shape of the data. It was a linear search wearing a loop.

The opposite mismatch is a single loop over data that is truly nested, often with manual index math to work out where each row starts. That is a flattened structure being rebuilt by hand, and reshaping the data once is easier than getting the math right at each use.

The general habit: write the loops that match the structure you've, and if they're awkward, change the structure rather than the math.

Measuring, rather than guessing

The cost of a nested loop can be read off the page, and when it matters it's still worth measuring, because the constant factors aren't visible in the form.

Two things make a reading handy. Run it at a few input sizes, not one: a single number tells you how long a thing took. Three tell you how the time grows, which is the property that decides whether the code lives larger data. Doubling the input should roughly double a linear loop and quadruple a nested one, and seeing that ratio confirms which you've.

And measure the thing itself, not the setup. timeit exists because a naive timing includes interpreter warm-up, the cost of building the test data, and what else happens to be in the block. It runs the snippet many times and reports the best, which is the number least polluted by all else on the machine.

The result is often surprising in the handy direction: a nested loop over fifty items is instant and not worth changing, while a linear loop that does a thing costly per item can be the real cost. Reading the structure tells you how it scales. Measuring tells you whether it matters yet.

Questions people ask about Nested For Loops

Can I use the same variable name in both loops? You can, and the inner one shadows the outer for the rest of the body. It's legal and confusing.

Does continue in the inner loop skip the outer one? No, it moves to the next inner iteration only.

How do I break out of both loops? Put them in a function and return, which is cleaner than any flag.

Is a nested loop always slow? No. It's slow when both loops grow with the input. A loop over n holding a loop over a fixed three items is linear.

What if the inner loop needs the outer index? enumerate at both levels gives you both, without any range(len(...)).

Why does my inner variable still have a value after the loop? Because a for loop doesn't have its own scope. The last value lives, unlike in a comprehension.

Is product faster than nested loops? Slightly, and that is not the reason to use it. It's flatter to read.

Should I worry about nesting depth for speed? Worry about how many times each loop runs, not how deep they're. Three shallow loops over three items each is nine iterations.

Can I nest a comprehension inside a loop? Yes, and it often reads well — the loop handles the structure and the comprehension handles one row.

Is there a limit on nesting depth? Python allows about twenty levels of indentation, which is far past readability. Treat three as the hands-on ceiling.

How do I loop over two grids together? zip the outer sequences, then loop the pair: for row_a, row_b in zip(a, b).

Nested For Loops: recap in one screen

  • The inner loop runs fully on each pass of the outer one. The bodies times rather than add.
  • Where the trailing print() is indented decides where rows end — the indentation is the logic, not the formatting.
  • break leaves one loop. A function and return is the clean way out of both.
  • Two loops over the same collection is n squared. An inner loop that is searching can often be replaced by a set or a dict.
  • itertools.product flattens loops that exist only to give combinations.

Check yourself

0 of 3

Answer without scrolling back up.

  1. Two loops nested over the same 500-item list. How many inner bodies run?

  2. A `break` in the inner loop of a nested pair does what?

  3. You double the size of the input to a doubly-nested loop. The work:

Cheat sheet

Nested For Loops

Run a loop inside a loop, trace the execution order step by step, and learn when a product of two ranges is the right structure.

PYTHON · vizlearn.in/python/nested_for_loops.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.