Loop Through Tuple
Tuples are immutable sequences, which means we can iterate/loop over each element of the tuple. Here we will discuss different ways by which we can iterate over the tuple.
For Loop
We can iterate over all the items in the tuple using a for loop. This is one of the simplest ways to iterate over the tuple. In the below example, we are using the for loop to iterate over each element of a birds tuple. Inside the for loop, we are printing each bird one by one.
birds_tuple = ("Parrot", "Crow", "Eagle")
for bird in birds_tuple:
print(bird)
For Loop using range() function – Loop through index numbers
The for loop can be used with the index positions of the tuple items to loop through the tuple. For this, the len() function is used to find out the length of the tuple. This length is passed to the range function to generate the sequence from 0 to the length of the tuple. Inside the “for” block, now we know the index position of the item, which can be used to find the item.
birds_tuple = ("Parrot", "Crow", "Eagle")
for i in range(len(birds_tuple)):
print(birds_tuple[i])
While loop
While loop can also be used to loop through the tuple. In the while loop, we manually have to add the stop condition, i.e. the condition at which the loop should stop execution and manually have to keep the track of the counter variable. Proper initialization and the increment of the counter variable has to be done by the user. Otherwise, because there is no stop condition for the while loop, it can become the infinite while loop and will never stop.
In the below example, we have initialized a variable “i” with index position 0. While loop will go up to the length of the birds’ tuple and, on every iteration, increase the value of the variable “i” by 1.
birds_tuple = ("Parrot", "Crow", "Eagle")
i = 0
while i < len(birds_tuple):
print(birds_tuple[i])
i = i + 1
Using enumerate()
The enumerate function is a built-in function which gives us more power while looping through the tuple because it returns both the index position and the element at that index location. It provides us the more Pythonic way to loop through the tuple.
The enumerate() function allows us to loop over any iterable (list, tuple, etc.), keeping track of both the index and the element at that index in each iteration. This is more useful when we want to know the position of an element within the tuple.
It returns pairs of (index, element), and we use a loop to unpack these pairs into the variables index and element.
birds = ("Parrot", "Crow", "Eagle")
for i, bird in enumerate(birds):
print(i, "-", bird)
Loop through list of Tuples
We will discuss the two ways by which we can loop through the list of tuples:
1. Using The for loop
The for loop can be used to iterate over the list of tuples. To achieve this, two loops are needed. One, to fetch the individual tuple from the list, and the second, to fetch the individual item from the tuple. It will be more clear from the below example.
employee_details = [("Elon", "Male", "57"),
("Bill", "Male", "55"),
("Lisa", "Female", "52")]
for i in employee_details:
for j in i:
print(j)
print("---------")
2. Using enumerate()
As enumerate() functions returns the tuple of index position and the item, from there we can utilize the item. A new “for” loop has to be used to loop through the individual element of the item returned by the enumerate() function.
employee_details = [("Elon", "Male", "57"),
("Bill", "Male", "55"),
("Lisa", "Female", "52")]
for i, employee in enumerate(employee_details):
for j in employee:
print(j)
print("---------")