Convert a list of tuples to two lists in Python

In this article, we will discuss different ways to convert a list of tuples into two lists in Python.

Table Of Contents

Introduction

Suppose we have a list of tuples. Like this,

# List of Tuples
listOfTuples = [('Mark', 34),
                ('John', 23),
                ('Ritu', 37),
                ('Avni', 29)]

We want to create two lists from this list of tuples. First list should have the first value of each tuple. Whereas, second list should have the second value of each tuple. Like this,

['Mark', 'John', 'Ritu', 'Avni']
[34, 23, 37, 29]

There are different ways to do this. Let’s discuss them one by one.

Advertisements

Method 1: Using map() and zip() functions

Diffuse all the tuples in list, by applying the astrik operator on the list. Then zip all the tuples together to create two sequences. Then apply list() function on both the sequences to convert both of them to lists (using map() function). This way we will have two lists. Let’s see an 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]

We created two lists from a list of tuples. First list has all values at index 0 from each tuple, and second list has all values at index 1 from each tuple.

Method 2: Using List Comprehension

Use a List comprehension to iterate through all the tuples in list, and for each tuple, add first value to the new list. This list will have first value of each tuple. Then use the same logic again to iterate through all the tuples in list, but this time select second value of each tuple, and add to a new list. This list will have second value of each tuple. Let’s see an 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]

We created two lists from a list of tuples. First list has all values at index 0 from each tuple, and second list has all values at index 1 from each tuple.

Method 3: Using zip() inside List Comprehension

Zip all the tuples together to create two sequences, and cast them to lists. Let’s see an 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]

We created two lists from a list of tuples. First list has all values at index 0 from each tuple, and second list has all values at index 1 from each tuple.

Summary

We learned about three different ways to create two list from a list of tuples in Python. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

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.

Scroll to Top