Learning LibraryCore Coding LibraryTeens

Nested loops in Python: every combination, covered

Three colors and four sizes make twelve products. Three growth rates and three prices make nine scenarios. Whenever the answer is "every combination," you need a loop inside a loop.

The big idea

A nested loop runs its entire inner loop once for every single pass of the outer loop.

See it in code

1The basics

For each color, the inner loop runs through every size before the outer loop moves on:

python
colors = ["SAGE", "OAT"]
sizes = ["S", "M", "L"]

for color in colors:
    for size in sizes:
        print(f"TEE-{color}-{size}")
Run it. Two colors, three sizes:
TEE-SAGE-S
TEE-SAGE-M
TEE-SAGE-L
TEE-OAT-S
TEE-OAT-M
TEE-OAT-L

Six SKUs. size cycled S, M, L twice, once for each color. The inner loop restarts every time the outer loop advances.

2A step further

Passes multiply. Count them and you get the size of the outer sequence times the size of the inner one:

python
colors = ["SAGE", "OAT", "CLAY", "NAVY"]
sizes = ["XS", "S", "M", "L", "XL"]

count = 0
for color in colors:
    for size in sizes:
        count = count + 1
print("SKUs to stock:", count)
Run it. How many products is that?
SKUs to stock: 20

4 × 5 = 20. That multiplication is why nested loops get expensive fast: 100 × 100 is already ten thousand passes.

3At Evergreen

Two planning assumptions, crossed. The outer loop picks a growth rate; for each one the inner loop tries every price, and an innermost loop runs the twelve months:

python
start_units = 300

for growth in [0.05, 0.10, 0.15]:
    for price in [32, 38, 44]:
        units = start_units
        year_one = 0
        for month in range(12):
            year_one = year_one + units * price
            units = round(units * (1 + growth))
        print(f"growth {growth:.0%}, price ${price}: ${year_one:,}")
Run it. Year-one revenue for nine scenarios:
growth 5%, price $32: $152,768
growth 5%, price $38: $181,412
growth 5%, price $44: $210,056
growth 10%, price $32: $205,088
growth 10%, price $38: $243,542
growth 10%, price $44: $281,996
growth 15%, price $32: $279,072
growth 15%, price $38: $331,398
growth 15%, price $44: $383,724

Read down the list. Raising the price by a third lifts revenue by a third, every time. Tripling the growth rate nearly doubles it. A grid like this shows which assumption deserves your attention.

The same idea, everywhere

The pattern is general. Compare every sequence against every other, test every strategy on every dataset, lay out every row and column of a page.

Try it yourself

Add 0.20 to the growth list and re-run. Then store each result in a list and print only the highest, so the grid finds its own best case.

The common mistake

Resetting a variable at the wrong level. If units = start_units sat outside the two outer loops, each scenario would begin where the last ended, and every number after the first would be wrong. Reset where each scenario begins.

What it unlocks

Nested loops are for loops stacked, usually driven by range() or lists, with an accumulator in the middle.

Want the simpler version? Read the Kids version →