PYTHON - Loop Tuples
Python - Loop Tuples
Loop Through Tuple Items
Looping through tuple items in Python refers to iterating over each element in atuplesequentially.
In Python we can loop through the items of atuplein various ways, with the most common being thefor loop. We can also use thewhile loopto 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 executeExample
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 100Loop 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 executeExample
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 += 1Output
Output of the above code is as follows β
1
2
3
4
5Loop 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