Loop Tuples in Python
Learn every way to loop through a Python tuple: for loops, while loops, enumerate, range(len()), zip, nested tuples, and loop control statements.
Looping through a tuple lets you visit each element one at a time and act on it. Because tuples are immutable sequences — their contents cannot change — iterating over them is safe and straightforward. This page covers every common technique: for loops, while loops, enumerate(), range(len()), zip(), nested-tuple traversal, and loop-control with break/continue.
If you are new to tuples, read Python Tuples first. For the general loop syntax see Python For Loops and Python While Loops.
Using a for Loop
The simplest and most Pythonic way to iterate over a tuple is a for loop. Python steps through each element in order and assigns it to the loop variable.
Loop through a tuple with a for loop
Output:
1
2
3
4
5The loop ends automatically when it reaches the last element — no index arithmetic required.
Using a while Loop
A while loop gives you manual control over the index. It is useful when you need to advance or rewind by more than one step, or when the stopping condition depends on something other than the tuple length.
Loop through a tuple with a while loop
my_tuple = ('apple', 'banana', 'cherry')
i = 0
while i < len(my_tuple):
print(my_tuple[i])
i += 1Output:
apple
banana
cherryAlways increment i inside the loop body, otherwise the loop runs forever.
Accessing the Index with enumerate()
When you need both the position and the value, use enumerate(). It wraps each element with its zero-based index and returns both together.
Loop through a tuple using enumerate
colors = ('red', 'green', 'blue')
for index, color in enumerate(colors):
print(index, color)Output:
0 red
1 green
2 blueYou can start counting from any number by passing a second argument:
Start enumerate from 1
colors = ('red', 'green', 'blue')
for index, color in enumerate(colors, start=1):
print(index, color)Output:
1 red
2 green
3 blueenumerate() is preferred over range(len()) whenever you need both index and value, because it is more readable and avoids the extra indexing step.
Accessing the Index with range(len())
An older but still valid pattern uses range(len()) to generate the valid indices, then accesses each element by position.
Loop through a tuple using range and len
fruits = ('apple', 'banana', 'cherry')
for i in range(len(fruits)):
print(i, fruits[i])Output:
0 apple
1 banana
2 cherryThis approach is handy when you need to compare adjacent elements or modify a derived list based on position, because both the index and the value are available at the same time.
Iterating Over Nested Tuples
A tuple can contain other tuples. Use a nested for loop to reach the inner values.
Iterate over a nested tuple (matrix)
matrix = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
for row in matrix:
for value in row:
print(value, end=' ')
print()Output:
1 2 3
4 5 6
7 8 9 Each iteration of the outer loop gives you one inner tuple (row). The inner loop then steps through that inner tuple element by element.
You can also unpack the inner tuple directly in the loop header if the shape is fixed:
Unpack inner tuples while looping
points = ((0, 0), (1, 2), (3, 4))
for x, y in points:
print(f'x={x}, y={y}')Output:
x=0, y=0
x=1, y=2
x=3, y=4This makes the code read almost like plain English and avoids row[0] / row[1] subscripts. For more on unpacking, see Unpack Tuples.
Looping Over Two Tuples in Parallel with zip()
zip() pairs up elements from two (or more) tuples by position and lets you unpack them in one loop.
Loop over two tuples in parallel
names = ('Alice', 'Bob', 'Carol')
scores = (95, 87, 92)
for name, score in zip(names, scores):
print(name, score)Output:
Alice 95
Bob 87
Carol 92zip() stops as soon as the shortest tuple is exhausted, so mismatched lengths do not raise an error — they are silently truncated. If you need all pairs and want to fill gaps, use itertools.zip_longest() from the standard library instead.
Loop Control: break and continue
Stopping Early with break
break exits the loop immediately when a condition is met.
Stop a tuple loop early with break
numbers = (1, 2, 3, 4, 5, 6)
for n in numbers:
if n == 4:
break
print(n)Output:
1
2
3The loop prints 1, 2, 3, then stops when it encounters 4.
Skipping Elements with continue
continue jumps to the next iteration without executing the remaining code in the loop body.
Skip even numbers using continue
numbers = (1, 2, 3, 4, 5, 6)
for n in numbers:
if n % 2 == 0:
continue
print(n)Output:
1
3
5Only the odd numbers are printed; even numbers are silently skipped.
Building a List from a Tuple Loop
Because tuples are immutable, you cannot change them in place. A common pattern is to iterate over a tuple and collect results in a new list.
Create a list of squares from a tuple
numbers = (1, 2, 3, 4, 5)
squares = [n ** 2 for n in numbers]
print(squares)Output:
[1, 4, 9, 16, 25]The list comprehension iterates over the tuple just like a for loop and is more concise for simple transformations. See Python Tuples and Tuple Methods for more on working with tuple data.
When to Use Each Technique
| Goal | Best approach |
|---|---|
| Visit every element in order | for item in my_tuple |
| Need the index too | enumerate() |
| Need precise index control | while loop or range(len()) |
| Iterate two tuples together | zip() |
| Traverse rows in a tuple of tuples | nested for loops |
| Stop early or skip elements | break / continue |