How to Loop Through a Tuple in Python

Discover how to loop through items in a tuple using Python's for loop. Learn how to iterate over tuple elements and display them with simple examples.



Loop Through a Tuple

You can loop through the items in a tuple using a for loop:

Example

  thistuple = ("apple", "banana", "cherry")
  for x in thistuple:
      print(x)
              
Output

  apple
  banana
  cherry
              

Learn more about for loops in our Python For Loops Chapter.

Loop Through the Index Numbers

You can also loop through tuple items by referring to their index numbers. Use the range() and len() functions to create an iterable:

Example

  thistuple = ("apple", "banana", "cherry")
  for i in range(len(thistuple)):
      print(thistuple[i])
              
Output

  apple
  banana
  cherry
              

Using a While Loop

You can loop through tuple items using a while loop. Use the len() function to determine the length of the tuple, then start at 0 and loop through the tuple items by referring to their indexes. Remember to increase the index by 1 after each iteration:

Example

  thistuple = ("apple", "banana", "cherry")
  i = 0
  while i < len(thistuple):
      print(thistuple[i])
      i = i + 1
              
Output

  apple
  banana
  cherry
              

Learn more about while loops in our Python While Loops Chapter.