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:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/tuple/python-tuple-exercise-17.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics