Python - Loop Tuples



Loop Through Tuple Items

Looping through tuple items in Python refers to iterating over each element in a tuple sequentially.

In Python we can loop through the items of a tuple in various ways, with the most common being the for loop. We can also use the while loop to iterate through tuple items, although it requires additional handling of the loop control variable explicitly i.e. an index.

Loop Through Tuple Items with For Loop

A for loop in Python is used to iterate over a sequence (like a list, tuple, dictionary, string, or range) or any other iterable object. It allows you to execute a block of code repeatedly for each item in the sequence.

In a for loop, you can access each item in a sequence using a variable, allowing you to perform operations or logic based on that item's value. We can loop through tuple items using for loop by iterating over each item in the tuple.

Syntax

Following is the basic syntax to loop through items in a tuple using a for loop in Python −

for item in tuple:
   # Code block to execute

Example

In the following example, we are using a for loop to iterate through each element in the tuple "tup" and retrieving each element followed by a space on the same line −

tup = (25, 12, 10, -21, 10, 100)
for num in tup:
   print (num, end = ' ')

Output

Following is the output of the above code −

25 12 10 -21 10 100 

Loop Through Tuple Items with While Loop

A while loop in Python is used to repeatedly execute a block of code as long as a specified condition evaluates to "True".

We can loop through tuple items using while loop by initializing an index variable, then iterating through the tuple using the index variable and incrementing it until reaching the end of the tuple.

An index variable is used within a loop to keep track of the current position or index in a sequence, such as a tuple or array. It is generally initialized before the loop and updated within the loop to iterate over the sequence.

Syntax

Following is the basic syntax for looping through items in a tuple using a while loop in Python −

while condition:
   # Code block to execute

Example

In the below example, we iterate through each item in the tuple "my_tup" using a while loop. We use an index variable "index" to access each item sequentially, incrementing it after each iteration to move to the next item −

my_tup = (1, 2, 3, 4, 5)
index = 0

while index < len(my_tup):
   print(my_tup[index])
   index += 1

Output

Output of the above code is as follows −

1
2
3
4
5

Loop Through Tuple Items with Index

An index is a numeric value representing the position of an element within a sequence, such as a tuple, starting from 0 for the first element.

We can loop through tuple items using index by iterating over a range of indices corresponding to the length of the tuple and accessing each element using the index within the loop.

Example

This example initializes a tuple "tup" with integers and creates a range of indices corresponding to the length of the tuple. Then, it iterates over each index in the range and prints the value at that index in the tuple "tup" −

tup = (25, 12, 10, -21, 10, 100)
indices = range(len(tup))
for i in indices:
   print ("tup[{}]: ".format(i), tup[i])

Output

We get the output as shown below −

tup[0]: 25
tup[1]: 12
tup[2]: 10
tup[3]: -21
tup[4]: 10
tup[5]: 100
Advertisements