w3resource

Python Exercise: Unzip a list of tuples into individual lists

Python tuple: Exercise-17 with Solution

Write a Python program to unzip a list of tuples into individual lists.

Sample Solution:

Python Code:

# Create a list of tuples, where each tuple contains two elements.
l = [(1, 2), (3, 4), (8, 9)]

# Use the 'zip' function with the '*' operator to unpack and zip the tuples.
# This creates new tuples where the first elements from the original tuples are combined into one tuple,
# and the second elements from the original tuples are combined into another tuple.
result = list(zip(*l))

# Print the result, which is a list of two tuples formed by zipping the original tuples.
print(result)

Sample Output:

[(1, 3, 8), (2, 4, 9)] 

Flowchart:

Flowchart: Unzip a list of tuples into individual lists

Python Code Editor:

Previous: Write a Python program to convert a tuple to a dictionary.
Next: Write a Python program to reverse a tuple.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.