B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Loop Through Tuples in Python with Examples | LiveCodeProgramming

Loop Through Tuples in Python

You can loop through a tuple using a for loop. It’s useful to access all items one by one.

Tuple के सभी items को एक-एक करके access करने के लिए for loop का इस्तेमाल किया जाता है।

1. Simple Tuple Loop
students = ("Abhishek", "Chaitanya", "Naincy", "Shrutika", "Jayati", "Aryan")
for name in students:
    print(name)
Output:
Abhishek
Chaitanya
Naincy
Shrutika
Jayati
Aryan
2. Loop with Index
for i in range(len(students)):
    print(i, students[i])
Output:
0 Abhishek
1 Chaitanya
2 Naincy
3 Shrutika
4 Jayati
5 Aryan
3. Using enumerate()
for index, name in enumerate(students):
    print(index, name)
Output:
0 Abhishek
1 Chaitanya
2 Naincy
3 Shrutika
4 Jayati
5 Aryan
4. Nested Tuple Loop
marks = (("Abhishek", 90), ("Naincy", 85), ("Aryan", 88))
for name, score in marks:
    print(name, "scored", score)
Output:
Abhishek scored 90
Naincy scored 85
Aryan scored 88