Open In App

Merge Two Lists into List of Tuples - Python

Last Updated : 13 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article

Similar Reads