Merge Two Lists into List of Tuples - Python
Last Updated :
13 Feb, 2025
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')].
Using zip()
zip() is the most efficient and recommended approach for merging two lists into tuples. It pairs elements from multiple iterables based on their position and returns an iterator that generates tuples.
Python
# Define two lists to be merged
a = [1, 2, 3]
b = ['a', 'b', 'c']
res = list(zip(a, b))
print(res)
Output[(1, 'a'), (2, 'b'), (3, 'c')]
Explanation: zip() pairs elements from both lists into tuples and list() converts the result into [(1, 'a'), (2, 'b'), (3, 'c')] .
Using list comprehension
This method is a variation of zip() combined with a list comprehension. While it provides flexibility if further transformation is needed within the comprehension, it is often considered redundant when simply merging lists.
Python
# Define two lists to be merged
a = [1, 2, 3]
b = ['a', 'b', 'c']
res = [(x, y) for x, y in zip(a, b)]
print(res)
Output[(1, 'a'), (2, 'b'), (3, 'c')]
Explanation: zip() pairs elements from both lists and the list comprehension creates a list of tuples .
Using map()
map() applies a function to elements from multiple iterables. Combined with lambda, it can merge two lists into tuples. This is a functional programming approach but is less readable compared to zip().
Python
# Define two lists to be merged
a = [1, 2, 3]
b = ['a', 'b', 'c']
res = list(map(lambda x, y: (x, y), a, b))
print(res)
Output[(1, 'a'), (2, 'b'), (3, 'c')]
Explanation: map() with lambda pairs elements from both lists into tuples and list() converts the result into [(1, 'a'), (2, 'b'), (3, 'c')] .
Using for loop
This is the most basic and least efficient approach. It involves manually iterating over the indices of the lists and appending tuples to a result list. While flexible, it is slower compared to the other methods.
Python
# Define two lists to be merged
a = [1, 2, 3]
b = ['a', 'b', 'c']
res = [] # initialize empty list
for i in range(len(a)):
res.append((a[i], b[i]))
print(res)
Output[(1, 'a'), (2, 'b'), (3, 'c')]
Explanation: for loop iterates over indices using range(len(a)), combining a[i] and b[i] into tuples and appending them to res.
Similar Reads
Python | Convert list of tuples into list In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
3 min read
Python - Convert a list into tuple of lists When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists.For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this con
3 min read
Python - List of tuples to multiple lists 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
3 min read
Merge Multiple Lists into one List In this article, we are going to learn how to merge multiple lists into one list. extend() method is another simple way to merge lists in Python. It modifies the original list by appending the elements of another list to it. This method does not create a new list but instead adds elements to an exis
3 min read
Python | Merge two list of lists according to first element Given two list of lists of equal length, write a Python program to merge the given two lists, according to the first common element of each sublist. Examples: Input : lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']] lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']] Output : [[1, 'Alice', 'Delhi'],
6 min read
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
How to Zip two lists of lists in Python? zip() function typically aggregates values from containers. However, there are cases where we need to merge multiple lists of lists. In this article, we will explore various efficient approaches to Zip two lists of lists in Python. List Comprehension provides a concise way to zip two lists of lists
3 min read
Python - Merging duplicates to list of list We are given a list we need to merge the duplicate to lists of list. For example a list a=[1,2,2,3,4,4,4,5] we need to merge all the duplicates in lists so that the output should be [[2,2],[4,4,4]]. This can be done by using multiple functions like defaultdict from collections and Counter and variou
3 min read
Python - Convert List of Lists to Tuple of Tuples Sometimes, while working with Python data, we can have a problem in which we need to perform interconversion of data types. This kind of problem can occur in domains in which we need to get data in particular formats such as Machine Learning. Let us discuss certain ways in which this task can be per
8 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