Python Loop Through Tuples | Meaning & 10 Examples

Python Loop Through Tuples

Looping through tuples means iterating over each element in a tuple one by one. It allows you to access and process each item inside a tuple easily using loops like for. This is useful when you want to read or perform actions on tuple data.

Tuple के अंदर के elements को एक-एक करके access करना loop through करना कहलाता है। इससे आप for जैसे loops का उपयोग करके tuple के हर item को आसानी से पढ़ और process कर सकते हैं। यह तब उपयोगी होता है जब आप tuple data पर कोई कार्य करना चाहते हैं।

Why loop through tuples?

  • To access each element in an ordered and immutable collection.
  • To perform repetitive actions on each tuple element.
  • When tuple contains multiple data you want to process individually.
  • Useful in situations where data is fixed but needs inspection or display.
  • Ordered और immutable collection के हर element को access करने के लिए।
  • Tuple के हर element पर बार-बार कोई action करने के लिए।
  • जब tuple में कई data होते हैं जिन्हें अलग-अलग process करना होता है।
  • जब data fixed होता है पर उसे जांचना या दिखाना होता है।
Example 1: Basic For Loop Over Tuple
t = (10, 20, 30)
for item in t:
    print(item)

Loop through each element and print it.

हर element को loop में लेकर print करें।

Output:

10
20
30
Example 2: Loop With Index Using enumerate()
t = ('a', 'b', 'c')
for index, value in enumerate(t):
    print(f'Index {index}: {value}')

Loop through tuple with index using enumerate().

enumerate() से index के साथ loop करें।

Output:

Index 0: a
Index 1: b
Index 2: c
Example 3: Sum Elements in Tuple Using Loop
t = (1, 2, 3, 4)
sum_val = 0
for num in t:
    sum_val += num
print('Sum:', sum_val)

Calculate sum of all elements by looping.

Loop से सभी elements का sum निकालें।

Output:

Sum: 10
Example 4: Loop Through Tuple of Strings
t = ('apple', 'banana', 'cherry')
for fruit in t:
    print(fruit.upper())

Loop and convert each string to uppercase.

हर string को uppercase में बदलें।

Output:

APPLE
BANANA
CHERRY
Example 5: Nested Loop Through Tuple of Tuples
t = ((1, 2), (3, 4), (5, 6))
for pair in t:
    for num in pair:
        print(num, end=' ')
    print()

Loop through nested tuples and print all numbers.

Nested tuples में loop कर सभी numbers print करें।

Output:

1 2
3 4
5 6
Example 6: Loop With Conditional Check
t = (10, 15, 20, 25)
for num in t:
    if num % 10 == 0:
        print(num, 'is divisible by 10')

Print elements divisible by 10.

10 से divisible elements को print करें।

Output:

10 is divisible by 10
20 is divisible by 10
Example 7: Loop With While and Index
t = (5, 10, 15)
i = 0
while i < len(t):
    print(t[i])
    i += 1

Loop through tuple using while loop and index.

while loop और index से tuple में loop करें।

Output:

5
10
15
Example 8: Using List Comprehension to Loop Tuple
t = (1, 2, 3, 4)
squares = [x**2 for x in t]
print(squares)

Create list of squares by looping tuple using list comprehension.

list comprehension से tuple के elements का square list बनाएं।

Output:

[1, 4, 9, 16]
Example 9: Loop Through Tuple and Create Dictionary
t = ('a', 1, 'b', 2)
dict_data = {}
for i in range(0, len(t), 2):
    dict_data[t[i]] = t[i+1]
print(dict_data)

Loop tuple in steps to create key-value pairs in dict.

Tuple में step से loop कर dict बनाएं।

Output:

{'a': 1, 'b': 2}
Example 10: Loop Tuple and Break on Condition
t = (1, 2, 3, 4, 5)
for num in t:
    if num == 4:
        break
    print(num)

Stop loop when element is 4.

Element 4 पर loop रोकें।

Output:

1
2
3