Python - List of tuples to multiple lists
Last Updated :
15 Jan, 2025
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'].
Similar Reads
Convert List of Tuples To Multiple Lists in Python When working with data in Python, it's common to encounter situations where we need to convert a list of tuples into separate lists. For example, if we have a list of tuples where each tuple represents a pair of related data points, we may want to split this into individual lists for easier processi
3 min read
Python | Convert list of tuples to list of list Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples.Using numpyNumPy makes it easy to convert a list of tu
3 min read
How to Multiply all items in Tuple - Python We are given a tuple of numbers and our task is to find the product of all elements. Since tuples are immutable hence we cannot modify them directly but we can iterate over the elements and compute the result efficiently. For example, given: tup = (2, 3, 5) then the output will be 30 as 2 Ã 3 Ã 5 =
2 min read
Merge Two Lists into List of Tuples - Python The task of merging two lists into a list of tuples involves combining corresponding elements from both lists into paired tuples. For example, given two lists like a = [1, 2, 3] and b = ['a', 'b', 'c'], the goal is to merge them into a list of tuples, resulting in [(1, 'a'), (2, 'b'), (3, 'c')]. Usi
3 min read
Sort Tuple of Lists in Python The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [
3 min read