How to use for loop to iterate over the items of a tuple in Python? In Python, tuples are iterable objects, which means you can use a for loop to iterate over the items in a tuple. When you use loops to iterate over a tuple, it returns an iterator object that yields each item of the tuple during each respective iteration.
1. Quick Examples of Iterating Tuple using For Loop
If you are in a hurry, below are some quick examples of iterating tuples for loop.
# Quick examples of iterating tuple using for loop
# Example 1: Iterate over tuple using for loop
tuples = ("Python", "Spark", "pandas")
for item in tuples:
# Example 2: Iterate over the tuple
# Using enumerate() function
tuples = ("Python", "Spark", "pandas")
for index, value in enumerate(tuples):
# Example 3: Using a while loop
tuples = ("Python", "Spark", "pandas", "Java")
index = 0
while index < len(tuples):
print(tuples[index])
index = index + 1
# Example 4: Tuple for loop
tuples = (2, 4, 6, 8, 10)
sum = 0
for num in tuples:
sum += num
# Example 5: Loop through the index numbers
tuples = ("Python", "Spark", "pandas", "Java")
for index in range(len(tuples)):
print(tuples[index])
2. Iterate over Tuple using For Loop
You can iterate over a tuple using a for loop
in Python, it loops over each item in the tuple and executes the intended code block once for each item. For example, the below example iterates over each element in tuples, and prints each elements from the tuple to the console.
# Iterate over tuple using for loop
tuples = ("Python", "Spark", "pandas")
for item in tuples:
print(item)
# Output:
# Python
# Spark
# pandas
You can also use the enumerate()
function to iterate over the tuple and get both the index and value of each item. For example, the enumerate()
function returns a sequence of tuples, where each tuple contains an index
and a value
from tuples. The for loop
iterates over each tuple and unpacks the index and value into the variable’s index and value.
# Iterate over the tuple
# Using enumerate() function
tuples = ("Python", "Spark", "pandas")
for index, value in enumerate(tuples):
print(index, value)
# Output:
# 0 Python
# 1 Spark
# 2 pandas
3. Using a While Loop
You can also use a while loop
to iterate over the elements of a tuple in python. The while loop continues to execute as long as the value of the index is less than the length of the tuples
.
In the below example, inside the loop, the print()
statement displays the value of the element at the current index in tuples
, and then the index is incremented by 1
. This process is repeated until the index is equal to the length of tuples, at which point the loop terminates.
# Using a while loop
tuples = ("Python", "Spark", "pandas", "Java")
index = 0
while index < len(tuples):
print(tuples[index])
index = index + 1
# Output:
# Python
# Spark
# pandas
# Java
4. Use Loop Through the Index Numbers
Similarly, you can also loop through the elements of a tuple using a for loop
with range() in Python. You can use the built-in range()
function along with the len()
function to specify the range of indices to loop over.
# Loop through the index numbers
tuples = ("Python", "Spark", "pandas", "Java")
for index in range(len(tuples)):
print(tuples[index])
# Output:
# Python
# Spark
# pandas
# Java
Conclusion
In this article, I have explained how to use for loop to iterate over the items of a tuple in Python with examples like using range(), enumeration() e.t.c.
Happy Learning !!
Related Articles
- range() in while loop in Python
- range() in for loop in Python
- Python For Loop in Backwards
- Python For Loop with Else Statement
- Nested For Loops in Python
- Python Catch Multiple Exceptions
- Python Return Tuple from Function
- Python Tuple index() Method