This tutorial will discuss about unique ways to convert a list of tuples to two lists in Python.
Table Of Contents
Introduction
Suppose we have a list of tuples, and each tuple has two elements i.e.
listOfTuples = [('Mark', 34), ('John', 23), ('Ritu', 37), ('Avni', 29)]
We want to convert this list of tuples into two lists. First list will have first value from each tuple, and second list will have second value from each tuple i.e.
['Mark', 'John', 'Ritu', 'Avni'] [34, 23, 37, 29]
Basically we want to convert a list of tuples to two lists column wise.
There are different ways to do that. Let’s discuss them one by one
Method 1: Using zip() and map() functions
Apply astrik on list to convert all tuples in list as individual parameters, and pass them to the zip() function. It will returned a zipped object containing two sequences internally. First sequence will have all the first elements from each tuple, second sequence will have all the second elements from each tuple. Then apply list() method on each sequence in the zipped object returned by the zip(). It will give a tuple of two lists.
Frequently Asked:
So, in the end we will get two lists. First list will have the first values from each tuple, and second list will have the second values from each tuple.
Let’s see the complete example,
# List of Tuples listOfTuples = [('Mark', 34), ('John', 23), ('Ritu', 37), ('Avni', 29)] # Convert a list of tuples into two lists firstList, secondList = list(map(list, zip(*listOfTuples))) print(firstList) print(secondList)
Output
['Mark', 'John', 'Ritu', 'Avni'] [34, 23, 37, 29]
Method 2: Using List Comprehension
Steps are as follows,
- Iterate over all tuples in list using a list comprehension, and construct a list from the first elements of each tuple.
- Iterate over all tuples in list using a list comprehension, and construct a list from the second elements of each tuple.
This way, we will have two lists. First list will have first values from each tuple, and second list will have second values from each tuple.
Let’s see the complete example,
# List of Tuples listOfTuples = [('Mark', 34), ('John', 23), ('Ritu', 37), ('Avni', 29)] # Convert a list of tuples into two lists firstList = [tupleObj[0] for tupleObj in listOfTuples] secondList = [tupleObj[1] for tupleObj in listOfTuples] print(firstList) print(secondList)
Output
['Mark', 'John', 'Ritu', 'Avni'] [34, 23, 37, 29]
Method 3: Using List Comprehension
This solution is similar to the first solution, but instead of map() function, we are using list comprehension to create a list from the sequences in the zipped object.
It will give us two lists. First list will have the first values from each tuple, and second list will the have second values from each tuple.
Let’s see the complete example,
# List of Tuples listOfTuples = [('Mark', 34), ('John', 23), ('Ritu', 37), ('Avni', 29)] # Convert a list of tuples into two lists firstList, secondList = [list(tupleObj) for tupleObj in zip(*listOfTuples)] print(firstList) print(secondList)
Output
['Mark', 'John', 'Ritu', 'Avni'] [34, 23, 37, 29]
Summary
We learned about three ways to convert a list of tuples into two lists in Python. Thanks.