Why Loop Through Tuples?

Looping through tuples allows you to:

  • Access each item one by one

  • Perform operations on every element

  • Display values for reporting or analysis

  • Combine tuple data with other logic

1. Looping with a for Loop

The most common way to traverse a tuple is using a for loop

Python
fruits = ("apple", "banana", "cherry") for fruit in fruits: print(fruit) # Output: # apple # banana # cherry

Here, each item in fruits is accessed in order.

2. Looping with Tuple of Numbers

You can loop through a tuple with numbers just the same

Python
values = (10, 20, 30, 40) for num in values: print(num) # Output: # 10 # 20 # 30 # 40

3. Looping Using Index

Though for ... in ... is easier, you can also loop using indexes

Python
colors = ("red", "blue", "green") for i in range(len(colors)): print(colors[i]) # Output: # red # blue # green

The range() function helps iterate over tuple positions.

4. Looping Nested Tuples

If a tuple contains other tuples (nested), you can loop through them too

Python
data = ((1, 2), (3, 4), (5, 6)) for pair in data: for num in pair: print(num) # Output: # 1 # 2 # 3 # 4 # 5 # 6

Nested loops help access inner values.

5. Looping with Conditional Logic

You can include conditions inside the loop

Python
nums = (1, 2, 3, 4, 5) for x in nums: if x % 2 == 0: print(x, "is even") # Output: # 2 is even # 4 is even

This checks and prints only even numbers.

6. Breaking Out of the Loop

Use break to stop looping early

Python
nums = (2, 4, 6, 8) for x in nums: if x == 6: break print(x) # Output: # 2 # 4

When x equals 6, the loop stops.

7. Skipping Items with continue

Use continue to skip an iteration

Python
nums = (1, 2, 3, 4) for x in nums: if x % 2 != 0: continue print(x) # Output: # 2 # 4

This only prints even numbers.

Important Notes

  • Tuples are immutable, but you can use loops to read values.

  • Loops process items in order from first to last.

  • Use nested loops for deeper structures.