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