Horje
Use For Loop to Iterate Tuple in Python

In this article, we will explore how to use a for loop to iterate through tuples in Python, along with some practical examples to illustrate its utility.

Use For Loop to Iterate Tuple in Python

Below are the ways by which we can use for loop to iterate Tuple in Python:

  • Using Simple for loop
  • Tuple of Tuple
  • Nested Tuples
  • Using enumerate functions
  • Loop through index numbers
  • Loop through tuple elements in a range

Using Simple For Loop

In the example, we created a tuple my_tuple containing five integers. We then used a for loop to iterate through each element of the tuple, and within the loop, we printed each element.

Python3

Output

1
2
3
4
5



Tuple of Tuple with For Loop

Here, we have a tuple of tuples containing student names and their ages. The for loop unpacks each tuple into name and age variables, allowing us to print student information.

Python3

student_info = (("Alice", 23), ("Bob", 20), ("Charlie", 25))
 
for name, age in student_info:
    print(f"{name} is {age} years old.")

Output

Alice is 23 years old.
Bob is 20 years old.
Charlie is 25 years old.



Nested Tuples with For Loop

This example shows how to iterate through a tuple of tuples, where each inner tuple contains a student’s name and their scores. We calculate and print the average score for each student.

Python3

students = (("Alice", (95, 88, 92)), ("Bob", (78, 84, 90)),
            ("Charlie", (88, 92, 89)))
 
for name, scores in students:
    avg_score = sum(scores) / len(scores)
    print(f"{name}'s average score: {avg_score:.2f}")

Output

Alice's average score: 91.67
Bob's average score: 84.00
Charlie's average score: 89.67



Using enumerate function

The enumerate function is useful when you want to access both the index and the value of each tuple element.

Python3

my_tuple = (1, 2, 3, 4, 5)
 
# Using enumerate
for index, value in enumerate(my_tuple):
    print(f"Index: {index}, Value: {value}")

Output

Index: 0, Value: 1
Index: 1, Value: 2
Index: 2, Value: 3
Index: 3, Value: 4
Index: 4, Value: 5



Loop through index numbers

You can also loop through the index numbers of a tuple and access the elements using indexing.

Python3

my_tuple = (1, 2, 3, 4, 5)
 
# Loop through index numbers
for index in range(len(my_tuple)):
    value = my_tuple[index]
    print(f"Index: {index}, Value: {value}")

Output

Index: 0, Value: 1
Index: 1, Value: 2
Index: 2, Value: 3
Index: 3, Value: 4
Index: 4, Value: 5



Loop through tuple elements in a range

You can loop through the elements of a tuple using a range and access them directly.

Python3

my_tuple = (1, 2, 3, 4, 5)
 
# Loop through tuple elements in a range
for value in my_tuple:
    print(f"Value: {value}")

Output

Value: 1
Value: 2
Value: 3
Value: 4
Value: 5






Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
Query Expressions in Django Query Expressions in Django
Debugging JavaScript in Microsoft Edge Browser Debugging JavaScript in Microsoft Edge Browser
Detached Elements Tool in Microsoft Edge Browser Detached Elements Tool in Microsoft Edge Browser
Device Emulation in Microsoft Edge Browser Device Emulation in Microsoft Edge Browser
Redundancy | System Design Redundancy | System Design

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12