liveBook · Manning

Nested loop

As explained in Learn AI-Assisted Python Programming, Second Edition

A nested loop is a loop whose body contains another loop, causing the inner loop to complete its iterations for each iteration of the outer loop.

A nested loop places one loop inside another. For each outer-loop iteration, the inner loop completes all its iterations before the outer loop advances. section 5.1.2

Nested loops commonly process two-dimensional data arranged as rows and columns. In Python, a list of lists can represent that structure, with indexes selecting a row and then a column. section 5.1.2

Pairing every country

With countries set to Canada, USA, and Japan, the inner loop prints Canada Canada, Canada USA, and Canada Japan before the outer loop proceeds to USA. The complete output contains every ordered pair.

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains execution order and list-of-lists traversal, but stops short of performance analysis.
Examples
Uses country pairs and an Olympic-medals list to demonstrate nested iteration.
What it assumes
Assumes familiarity with Python for loops, lists, range, len, and indexing.
What you can do after
Apply nested Python for loops to generate combinations and visit two-dimensional list values.
How it connects
Connects loops with two-dimensional data, lists, indexes, rows, and columns.

Also explained in

Compare how 3 books explain nested loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.