Open In App

Create a List of Tuples in Python

Last Updated : 14 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The task of creating a list of tuples in Python involves combining or transforming multiple data elements into a sequence of tuples within a list. Tuples are immutable, making them useful when storing fixed pairs or groups of values, while lists offer flexibility for dynamic collections. For example, given two separate lists like [1, 2, 3] and [‘apple’, ‘orange’, ‘cherry’], the goal is to combine them into a list of tuples like [(1, ‘apple’), (2, ‘orange’), (3, ‘cherry’)].

Using zip()

zip() is the most efficient approach to combine two or more separate lists into a list of tuples. It pairs elements from each list based on their index and stops when the shortest list ends. This method is optimized, making it fast and memory efficient and is preferred for combining data into tuples.

a = [1, 2, 3]
b = ['apple', 'orange', 'cherry']

res = list(zip(a, b))

print(res)

Output
[(1, 'apple'), (2, 'orange'), (3, 'cherry')]

Explanation: zip() pairs elements from lists a and b by index, creating tuples like (a[0], b[0]), (a[1], b[1]), etc. and returns an iterator, which is converted into a list using list().

Using map()

map() is the most efficient approach to convert a list of lists into a list of tuples. It applies the tuple() constructor to each sublist, transforming each inner list into a tuple. This approach is optimized, making it faster than list comprehension when working with nested lists. It is highly recommended when transforming list-based data into tuples.

a = [[1, 'apple'], [2, 'orange'], [3, 'cherry']]

res = list(map(tuple, a))
print(res)

Output
[(1, 'apple'), (2, 'orange'), (3, 'cherry')]

Explanation: map() converts each inner list in a into a tuple by applying tuple() to every sublist. It returns an iterator, which is converted into a list using list().

Using list comprehension

This method combines zip() with list comprehension to form a list of tuples. It is slightly less efficient than bare zip() due to the extra comprehension layer, but it offers more flexibility and readability when additional operations need to be performed while pairing data from two lists. It is frequently used for clarity in custom logic scenarios.

a = [1, 2, 3]
b = ['apple', 'orange', 'cherry']

res = [(x, y) for x, y in zip(a, b)]

print(res)

Output
[(1, 'apple'), (2, 'orange'), (3, 'cherry')]

Explanation :list comprehension pairs elements from a and b using zip(), forming (x, y) tuples and produces a list of tuples as the result.

Using for loop

This traditional approach involves iterating through two separate lists using a for loop and append() to build a list of tuples manually. While functional, it is less efficient compared to zip() because Python-level loops introduce overhead. This method is still useful when detailed control over the loop is required, but it is not recommended for simple tuple creation tasks.

a = [1, 2, 3]
b = ['apple', 'orange', 'cherry']

res = []
for i in range(len(a)):
    res.append((a[i], b[i]))

print(res)

Output
[(1, 'apple'), (2, 'orange'), (3, 'cherry')]

Explanation: for loop iterates over indices of a and b using range(len(a)), forming tuples (a[i], b[i]), and appends them to res.



Next Article

Similar Reads

three90RightbarBannerImg