What Is Iteration?
In programming, iteration is repeating a set of instructions, usually with a loop, until a condition is met or every item in a collection has been processed. Each single pass through the repeated instructions is also called an iteration.
Updated September 24, 2026
A program that sends a reminder email to 10,000 users does not contain 10,000 copies of the sending code. It contains the code once, inside a loop, and the loop runs it once per user. Each run is one iteration, and the loop as a whole is how the program iterates over its users.
How iteration works, step by step
Every loop follows the same cycle:
- Check. Test the loop's condition, which is a boolean: is there another item, or is the counter still below the limit?
- Run. If the answer is true, run the loop body once.
- Update. Move to the next item or change the counter.
- Repeat. Go back to step 1. When the check gives false, the program continues with the code after the loop.
Here the body runs five times, and each iteration adds the next number to a running total:
iteration 1 total is now 1
iteration 2 total is now 3
iteration 3 total is now 6
iteration 4 total is now 10
iteration 5 total is now 15
Done: 15
The variable total lives outside the loop, so it keeps its value from one iteration to the next. That pattern, a variable set before the loop and updated inside it, is called an accumulator, and it appears in almost every loop that computes something.
Counting loops and condition loops
There are two kinds of iteration. Definite iteration runs a known number of times: once per item in a list, or once for each number from 1 to 5. Python's for loop is built for this, often together with range().
Indefinite iteration repeats until something happens, and the number of passes is not known in advance. A while loop fits this case:
8 years, balance 214.36
Money that grows by 10% a year takes 8 years to double. The program found that out by iterating, not by solving an equation. The for loops and while loops guides cover both in Python.
Iterating over a collection
Most loops walk through a collection: the elements of an array, the characters of a string, the lines of a file. Python's for loop hands you each element directly, and enumerate() adds its position:
0 apple
1 banana
2 cherry
Behind the scenes, for asks the collection for an iterator with iter(), then calls next() on it once per iteration. You can make the same calls yourself:
red
green
blue
no more items
Without the default value, the fourth next() raises StopIteration. That exception is the signal a for loop uses to know the collection is finished.
Other languages spell the same idea differently. C counts with an index, while Java and JavaScript also have a loop that hands you each element:
for (int i = 0; i < 3; i++) {
printf("%s\n", colors[i]);
}
for (String color : colors) {
System.out.println(color);
}
for (const color of colors) {
console.log(color);
}
Iteration vs recursion
Recursion is the other way to repeat work: a function calls itself on a smaller version of the problem until it reaches a case it can answer directly. Anything written with one can be written with the other, but they behave differently:
| Iteration | Recursion | |
|---|---|---|
| How it repeats | A loop jumps back to its start | A function calls itself |
| When it stops | The loop condition becomes false | A base case is reached |
| Memory | The same amount for any number of passes | One stack frame for every call still open |
| Typical failure | An infinite loop | Stack overflow (RecursionError in Python) |
| Good fit | Lists, counters, repeating until a condition | Trees, nested data, divide and conquer |
The memory row matters in practice. Python allows about 1,000 nested calls by default, so a recursive sum of 5,000 numbers fails where a loop has no trouble:
5050 5050
12502500
sum_recursive(5000) failed: RecursionError
The recursion visualization shows the stack of calls growing and shrinking step by step.
Common mistakes
The infinite loop. A while loop whose condition never becomes false runs until you stop the program. The usual cause is forgetting the update step, such as leaving out count = count - 1.
Off by one. range(1, 5) gives 1, 2, 3 and 4, not 5, because the end value is excluded. A loop that runs one time too many or too few still runs without an error and gives a wrong answer, which is a logic error.
Changing a list while looping over it. Removing items from the list you are iterating over makes the loop skip elements:
[1, 2, 3]
One 2 survived, because removing the first 2 shifted the second one into a position the loop had already passed. Build a new list instead: numbers = [n for n in numbers if n != 2].
Where to go next
Loops walk through collections, so what an array is is the natural companion page, and what a boolean is explains the true or false checks that decide when a loop stops. When a loop gives the wrong result without an error message, read what a logic error is. To practice writing loops, start the Python course or try the examples above in the Python playground.
Frequently Asked Questions
What is an example of an iteration?
for loop that prints every name in a list of 100 users runs 100 iterations, one per name. A while loop that keeps asking for a password until the correct one is typed is also iteration, even though the number of passes is not known in advance.Which one is better, recursion or iteration?
Is iteration the same as a loop?
for or while, and iteration is the repetition it performs. A single pass through the loop body is also called an iteration, so a loop that runs 5 times performs 5 iterations.What is a sprint vs. iteration?
What does iterable mean?
for, such as a list, a string, a dictionary, a file or a range. In Python, an object is iterable if iter() can produce an iterator from it. Integers are not iterable, which is why for x in 5: raises a TypeError.