← Back to Chapters

Python Tuple Loops

? Python Tuple Loops

⚡ Quick Overview

Tuples in Python are ordered, immutable collections. Even though you cannot change their items, you can easily loop through them to read or process data. You can use:

  • for loops for simple iteration.
  • Index-based loops using range() and len().
  • while loops when you need more control over the index.
  • enumerate() to get both index and value together.
  • Nested loops for tuples that contain other tuples (nested tuples).

? Key Concepts

  • A tuple is iterable, so you can use it directly in a for loop.
  • Index-based loops are useful when position (index) matters.
  • while loops require you to manually control the index variable.
  • enumerate() is a Pythonic way to get index and value in one step.
  • Nested tuples can be traversed using nested loops (loop inside a loop).
  • Tuples are immutable: loops can read items but cannot modify them in-place.

? Syntax and Patterns

? Basic for Loop

Loop directly over each item in the tuple.

for item in my_tuple:

? Index-Based Loop

Use range() and len() to access items by index.

for i in range(len(my_tuple)):

? while Loop

Manually control the index variable.

while i < len(my_tuple):

? enumerate()

Get both index and value at the same time.

for index, value in enumerate(my_tuple):

? Code Examples

? Looping Through Tuples with for

The simplest way to iterate over a tuple is using a for loop. Each item is accessed one by one.

? View Code Example
fruits = ("apple", "banana", "cherry")


# Loop through each fruit in the tuple
for fruit in fruits:
    print(fruit)

? Output

apple
banana
cherry

? Looping Through Indexes

Use range() and len() when you need the index along with the item.

? View Code Example
fruits = ("apple", "banana", "cherry")


# Use the index to access each fruit
for i in range(len(fruits)):
    print(f"Index {i}: {fruits[i]}")

? Output

Index 0: apple
Index 1: banana
Index 2: cherry

? Using while Loop

A while loop gives you more control over how and when the index changes.

? View Code Example
fruits = ("apple", "banana", "cherry")

i = 0
# Iterate using a while loop and manual index
while i < len(fruits):
    print(f"Item {i}: {fruits[i]}")
    i += 1

? Output

Item 0: apple
Item 1: banana
Item 2: cherry

? Looping with enumerate()

enumerate() provides a clean way to get both index and value in each iteration.

? View Code Example
fruits = ("apple", "banana", "cherry")


# Use enumerate to get both index and value
for index, fruit in enumerate(fruits):
    print(f"Index {index} -> {fruit}")

? Output

Index 0 -> apple
Index 1 -> banana
Index 2 -> cherry

? Looping Through Nested Tuples

For tuples that contain other tuples (nested tuples), use nested for loops.

? View Code Example
matrix = ((1, 2), (3, 4), (5, 6))

# Outer loop goes through each inner tuple (row)
for row in matrix:
    # Inner loop goes through each value in the row
    for item in row:
        print(item, end=" ")
    print()

? Output

1 2
3 4
5 6

? Live Output and Explanation

In all the examples above, the loop visits each item in the tuple in order. For basic for loops, the loop variable (fruit, row, item) directly holds each element.

  • In the index-based loops, the variable i or index tracks position, and you use it to access items like fruits[i].
  • In the nested loop example, the outer loop goes through each inner tuple (row), and the inner loop goes through each value in that row.
  • Even though you are looping, the original tuple never changes because tuples are immutable.

? Helpful Tips

  • Prefer a simple for item in tuple loop for readability.
  • Use enumerate() when you need both index and value.
  • Use nested loops for nested tuples (e.g., a tuple of tuples).
  • Remember that tuples are immutable: do not try to reassign items inside the loop.
  • When using while, always update the index (e.g., i += 1) to avoid infinite loops.
  • Only use index-based loops when the index itself is required for your logic.

? Practice Exercises

  • Create a tuple of 5 numbers and use a for loop to print only the odd numbers.
  • Use enumerate() on a tuple of strings to display each item with its index.
  • Make a nested tuple (e.g., ((1, 2), (3, 4), (5, 6))) and print each inner element separately.
  • Write a while loop that iterates through a tuple and stops when a specific value is found.