Open In App

Python – List of tuples to multiple lists

Last Updated : 15 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Converting a list of tuples into multiple lists involves separating the tuple elements into individual lists. This can be achieved using methods like zip(), list comprehensions or loops, each offering a simple and efficient way to extract and organize the data.

Using zip()

zip() function is a concise and efficient way to separate the elements of each tuple into multiple lists.

Python
# List of tuples
l = [(1, 'a'), (2, 'b'), (3, 'c')]

# Convert to multiple lists using zip
l1, l2 = zip(*l)

# Convert the result to lists
list1 = list(l1)
list2 = list(l2)

print(list1)  
print(list2) 

Output
[1, 2, 3]
['a', 'b', 'c']

Explanation:

  • The zip(*l) unpacks the list of tuples l and pairs the first elements of each tuple together into l1, and the second elements into l2.
  • The list() function is then used to convert these zipped results (l1 and l2) into two separate lists, resulting in list1 = [1, 2, 3] and list2 = [‘a’, ‘b’, ‘c’].

Using List Comprehension

We can use list comprehension to manually extract the individual elements from each tuple.

Python
# List of tuples
l = [(1, 'a'), (2, 'b'), (3, 'c')]

# Convert to multiple lists using list comprehension
l1 = [x[0] for x in l]
l2 = [x[1] for x in l]

print(l1) 
print(l2)  

Output
[1, 2, 3]
['a', 'b', 'c']

Explanation:

  • The list comprehension [x[0] for x in l] iterates over each tuple in the list l, extracting the first element (the integers) and storing them in l1.
  • Similarly, [x[1] for x in l] extracts the second element (the strings) from each tuple and stores them in l2. The output will be l1 = [1, 2, 3] and l2 = [‘a’, ‘b’, ‘c’].

Using a Loop

We can manually loop through each tuple in the list and append the elements to separate lists.

Python
# List of tuples
l = [(1, 'a'), (2, 'b'), (3, 'c')]

# Create empty lists
l1, l2 = [], []

# Loop through each tuple and separate elements
for item in l:
    l1.append(item[0])
    l2.append(item[1])

print(l1)
print(l2)  

Output
[1, 2, 3]
['a', 'b', 'c']

Explanation:

  • The code iterates over each tuple in the list l, extracting the first element (item[0]) and appending it to the list l1, and extracting the second element (item[1]) and appending it to the list l2.
  • As a result, the lists l1 and l2 contain [1, 2, 3] and [‘a’, ‘b’, ‘c’], respectively.

Using map() Function

map() function can be used to apply a function to each tuple, extracting the elements and converting them to lists.

Python
# List of tuples
l = [(1, 'a'), (2, 'b'), (3, 'c')]

# Convert to multiple lists using map
l1, l2 = map(list, zip(*l))

print(l1)  
print(l2)  

Output
[1, 2, 3]
['a', 'b', 'c']

Explanation:

  • zip(*l) unpacks the list of tuples l, grouping the first elements of each tuple together in one iterable and the second elements together in another iterable.
  • map(list, …) function converts these iterables into lists, resulting in l1 = [1, 2, 3] and l2 = [‘a’, ‘b’, ‘c’].


Next Article

Similar Reads