In this tutorial, learn how to unpack tuple pairs in list with Python. The list contains tuple elements with two in pairs. Get each tuple with paired elements after unpacking using the methods given here.

The list is a similar element as that or array in other programming languages. Here, the list elements are tuple enclosed within the round bracket. The tuple elements are paired within the list with two element pairs.

Unpack Tuple Pairs in List With For Loop in Python

If you want to unpack the tuple pairs in list, you have to use the for loop to the list.

The example contains the 3 tuples as the list elements. To perform for loop with the list, you have to follow the below-given example.

Output

(‘Ram’, 23)
(10, ‘Dara’)
(17, ‘Raju’)

The above example contains the list with 3 tuple elements. Each tuple contains 2 elements in pairs. After you perform loop iteration with the list in Python, you will get individual tuples in the output.

Use While Loop to Unpack Tuple Pairs in List in Python

In addition to for loop, you can also unpack tuple pairs in the list by using while loop. To perform while loop with this list, you have to find the length of the list in Python to perform iteration.

After the iteration, you will get the same number of the tuple as you get with for loop above. See the example below to get the idea of how to use while loop to unpack the tuples given as list elements.

Output

(‘Ram’, 23)
(10, ‘Dara’)
(17, ‘Raju’)

The above example contains the list tuple with 2 elements in the pair. There are 3 elements of the list as tuple which gets printed as the single element in single line.

Unpack Tuple pairs in List Directly Without List Variable

Python comes with another method for developers to unpack tuple in list. To perform this task, you have to pass the list directly in the for loop of Python. See the example below contains the two line of code to unpack tuple in the list.

Output

(‘Ram’, 23)
(10, ‘Dara’)
(17, ‘Raju’)

The above example prints the same number of tuple elements. These elements are the same output which you get from the above given other methods.

You may also like to read

Hope, you like this post of how to unpack tuple pairs in list with Python. If you have any query regarding the tutorial, please comment below.

Also tell me, which method you are using to unpack the tuple in the given list using Python.

Leave a Reply

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.