CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

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 βˆ’

PYTHON CODE
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 βˆ’

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

Output

Following is the output of the above code βˆ’

PYTHON 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 βˆ’

PYTHON CODE
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 βˆ’

PYTHON CODE
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 βˆ’

PYTHON CODE
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” βˆ’

PYTHON CODE
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 βˆ’

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