Nested loop
A nested loop is a loop placed inside another loop so that the inner loop executes within each iteration of the outer loop.
Get Programming: Learn to code with Python
A nested loop is a loop placed inside another loop so that its execution occurs within each iteration of the outer loop.
- How it is used
- Uses nested loops to test every letter of each candidate word against available Scrabble tiles.
- How deep it goes
- Explains outer and inner responsibilities, early exit, duplicate handling, remaining state, and the final completion check.
- Examples
- Demonstrates word candidates, individual letters, tile removal, duplicate letters, and words such as art, hue, and color.
- What you can do after
- Build it from scratch by translating a two-level checking process into pseudocode and executable loop logic.
Read how Get Programming: Learn to code with Python explains nested loop →
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.
- How it is used
- Processes every combination or every cell in row-and-column data.
- 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 you can do after
- Apply nested Python for loops to generate combinations and visit two-dimensional list values.
Read how Learn AI-Assisted Python Programming, Second Edition explains nested loop →
CUDA for LLMs
A nested loop is a loop construct placed inside one or more other loop constructs to iterate over multiple dimensions or levels.
- How it is used
- Uses nested loops to express matrix operations and as a starting point for mapping independent work onto GPU threads.
- How deep it goes
- Traces loop roles through matrix addition, GEMM, and tiled attention, including which iterations are parallelized or retained.
- Examples
- Uses matrix addition, triple-loop GEMM, neural-network forward-pass operations, and nested Q/K/V tile loops in Flash Attention.
- What you can do after
- Build a naive GEMM kernel by assigning each output element to a thread while retaining its dot-product loop.
Practices of the Python Pro
A nested loop is a loop construct placed inside the body of another loop, causing repeated iteration across multiple levels.
- How it is used
- Uses nested loops to identify quadratic growth while analyzing algorithm performance.
- How deep it goes
- Explains the repeated comparisons, early termination, and worst-case O(n²) behavior without extending into optimization techniques.
- Examples
- Demonstrates duplicate detection with Python loops over a sequence.
- What you can do after
- Evaluate nested-loop code by recognizing its potential quadratic worst-case running time.
Also covered in
- Software Design for Python Programmers
- Classic Game Programming on the NES: Make your own retro video game
What to know first
- loopA loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
- indexingIndexing is the operation of selecting or accessing elements or regions of an ordered data structure by position, key, or index.
Related concepts
- CUDA kernelalternative to · A CUDA kernel is a function launched on a GPU for execution by multiple threads in parallel.
- matrix-matrix multiplicationused by · Matrix multiplication is an operation on compatible matrices in which each entry of the product is the dot product of a row of the first matrix and a column of the second.
- loopkind of · A loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
- wmma operationalternative to · A WMMA operation is a warp-level matrix multiply-accumulate that computes a fixed-size matrix tile using tensor-core hardware.
- Cartesian productequivalent to · The Cartesian product of sets is the set of ordered tuples formed by choosing one element from each set.
Questions about nested loop
What is nested loop?
A nested loop is a loop placed inside another loop so that the inner loop executes within each iteration of the outer loop.
Which Manning books explain nested loop?
Get Programming: Learn to code with Python, Learn AI-Assisted Python Programming, Second Edition, CUDA for LLMs and Practices of the Python Pro.
What should I know before learning nested loop?
Manning books that teach nested loop usually introduce loop and indexing first.
What is the difference between nested loop and CUDA kernel?
A nested loop is a loop placed inside another loop so that the inner loop executes within each iteration of the outer loop. A CUDA kernel is a function launched on a GPU for execution by multiple threads in parallel.
What is nested loop a kind of?
Nested loop is a kind of loop. A loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



