How to Iterate Through Tuple Elements Using Python

In this tutorial, learn how to iterate through tuple elements in Python. Get each individual element of tuple and print in the output with various looping methods.

Perform iteration with the for loop or the while loop to get individual elements. Check the sections given below with loop iteration and see the examples. If you don’t know about tuple then you may like to read how to create tuple in Python.

Iterate Through Tuple Elements Using For Loop in Python

You can perform the loop over the tuple elements using the for loop of Python. The for loop iterate each element of the tuple and print the elements in every single line. The elements can be of any type like string or integers.

Output

Ram
23
10
Dara
17
Raju

The above example showing the tuple elements with a single element in a single line. This is simple for loop method for iteration through the tuple elements.

However, you can also use the while loop to iterate through each element of the tuple. You have to read further in the section to know the method with while loop.

Loop Through Tuple Elements Using While Loop in Python

The while loop using the different method from the for loop method. You have to first initialize a variable with zero(0). After that, you have to find the length of tuple for continous iteration.

See the example below to find the method of looping with while loop in Python.

Output

Ram
23
10
Dara
17
Raju

The above example prints the same output as you get with the for loop of Python. However, the method is different from the method of for loop. You can use both the above-given methods as per your requirement.

Iteration With Tuple Directly Without Tuple Variable

In addition to above all method, you can also perform the iteration over tuple element without the tuple variable. To perform this task, you have to pass the tuple directly in the for loop as given below.

Output

Ram
23
10
Dara
17
Raju

The above example contains the same number of tuple elements as given above examples. This is the simple two line code to perform iteration over the tuple elements.

You may also like to read

Hope, you like this tutorial of how to iterate through the tuple elements with Python. If you have any query regarding the tutorial, please comment below.

Reference
Stackoverflow Discussion on Python For Tuple Iteration

Also tell me, which method you are using to loop through the tuple elements in Python.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.