Open In App

Convert List of Tuples To Multiple Lists in Python

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Managing data often entails working with tuples of information, particularly when working with datasets. For more easy processing, these tuples can sometimes need to be split up into numerous lists. In this article, we will see how to convert list of tuples to multiple lists in Python.

Convert List of Tuples to Multiple Lists in Python

Below are some of the ways by which we can convert a list of tuples into multiple lists in Python:

Convert List of Tuples to Multiple Lists Using map Function

In this example, the code transforms a list of tuples into two separate lists using map() and zip(). It creates ‘list1’ containing the first elements of the tuples and ‘list2’ containing the second elements, then prints both lists.

Python3




# List of Tuples
list_of_tuples = [(1, 'a'), (2, 'b'), (3, 'c')]
 
# Using map and zip
list1, list2 = map(list, zip(*list_of_tuples))
 
# Output
print("List 1:", list1)
print("List 2:", list2)


Output

List 1: [1, 2, 3]
List 2: ['a', 'b', 'c']



Convert List of Tuples to Multiple Lists Using Iterative Unpacking

In this example, the code employs iterative unpacking to transform a list of tuples into two separate lists. It iterates through each tuple, unpacks its elements into ‘val1’ and ‘val2’, and appends them to ‘list1’ and ‘list2’ respectively, then prints both lists.

Python3




# List of Tuples
list_of_tuples = [(1, 'a'), (2, 'b'), (3, 'c')]
 
# Iterative Unpacking
list1, list2 = [], []
for item in list_of_tuples:
    val1, val2 = item
    list1.append(val1)
    list2.append(val2)
 
# Output
print("List 1:", list1)
print("List 2:", list2)


Output

List 1: [1, 2, 3]
List 2: ['a', 'b', 'c']



Convert List of Tuples to Multiple Lists Using zip() Function

In this example, the code unpacks a list of tuples using `zip` and separates the elements into three lists: ‘numbers’ containing the first elements, ‘letters’ containing the second elements, and ‘booleans’ containing the third elements. The resulting lists are then printed.

Python3




# Given list of tuples
list_of_tuples = [(1, 'a', True), (2, 'b', False), (3, 'c', True)]
 
# Unpacking tuples using zip
numbers, letters, booleans = zip(*list_of_tuples)
 
# Displaying the resulting lists
print("Numbers:", list(numbers))
print("Letters:", list(letters))
print("Booleans:", list(booleans))


Output

Numbers: [1, 2, 3]
Letters: ['a', 'b', 'c']
Booleans: [True, False, True]



Converting List of Tuples with Different Lengths Using zip_longest() Function

In this example, the code uses zip_longest() from the itertools module to unpack a list of tuples with different lengths. It separates the elements into lists, such as ‘numbers,’ ‘letters,’ and ‘booleans,’ while handling varying tuple lengths. The resulting lists are then printed, and any additional elements are collected into the ‘extra’ list.

Python3




from itertools import zip_longest
 
# Given list of tuples with different lengths
list_of_tuples = [(1, 'a'), (2, 'b', True), (3, 'c', False, 'extra')]
 
# Unpacking tuples using zip_longest to handle different lengths
numbers, letters, booleans, * \
    extra = zip_longest(*list_of_tuples, fillvalue=None)
 
# Displaying the resulting lists
print("Numbers:", list(numbers))
print("Letters:", list(letters))
print("Booleans:", list(booleans))
print("Extra:", extra)


Output

Numbers: [1, 2, 3]
Letters: ['a', 'b', 'c']
Booleans: [None, True, False]
Extra: [(None, None, 'extra')]



Conclusion

In Python, converting a list of tuples to several lists is a simple operation; the method used will rely on readability and personal taste. The given examples demonstrate many approaches to get the intended result, allowing for flexibility in execution.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads