5 Best Ways to Create a List of Tuples From Two Lists

4.5/5 - (2 votes)

βœ… Problem Formulation: How to create a list of tuples by combining elements from two separate lists. Typically, each tuple is formed by taking one element from each list and pairing them together based on their position within their respective lists. For example, given List A: [1, 2, 3] and List B: ['a', 'b', 'c'], the desired output is to create a List of Tuples: [(1, 'a'), (2, 'b'), (3, 'c')].

Method 1: Using the Zip Function

The zip function in Python takes iterables (can be zero or more), aggregates them in a tuple, and returns it. zip is often used to map the similar index of multiple containers so that they can be used just using a single entity. It’s a straightforward and Pythonic way to pair elements from two lists into a list of tuples.

Here’s an example:

list_a = [1, 2, 3]
list_b = ['a', 'b', 'c']
list_of_tuples = list(zip(list_a, list_b))
print(list_of_tuples)

In this code snippet, the zip function iterates over the two lists and aggregates corresponding elements into tuples, effectively pairing each number from list_a with a letter from list_b. The list function is then used to convert the iterable returned by zip into a list of tuples.

Method 2: List Comprehension with Zip

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. By combining list comprehension with the zip function, you get a compact and efficient way to create a list of tuples from two lists.

Here’s an example:

list_a = [4, 5, 6]
list_b = ['d', 'e', 'f']
list_of_tuples = [(a, b) for a, b in zip(list_a, list_b)]
print(list_of_tuples)

The code snippet above showcases the use of list comprehension to create a list of tuples. It pairs elements from list_a and list_b by iterating over the two lists simultaneously using the zip function within the list comprehension.

Method 3: Using a For Loop

For those who are just beginning with Python or who prefer a more procedural approach over functional or list comprehension methods, a for loop can be used to iterate explicitly over both lists and form tuples.

Here’s an example:

list_a = [7, 8, 9]
list_b = ['g', 'h', 'i']
list_of_tuples = []

for i in range(len(list_a)):
    list_of_tuples.append((list_a[i], list_b[i]))

print(list_of_tuples)

The code above uses a traditional for loop to traverse the indices of list_a and list_b. At each index, it appends a new tuple to list_of_tuples consisting of the ith element from each list.

Method 4: Using the map() and lambda Functions

The map() function applies a given function to each item of an iterable (list, tuple, etc.) and returns a list of the results. In conjunction with a lambda function, map can be used to pair items from two lists into tuples.

Here’s an example:

list_a = [10, 11, 12]
list_b = ['j', 'k', 'l']
list_of_tuples = list(map(lambda a, b: (a, b), list_a, list_b))
print(list_of_tuples)

In this example, the map() function is used with a lambda function that takes two arguments (from list_a and list_b) and creates a tuple out of them. The list() function is then used to convert the map object into a list of tuples.

Bonus One-Liner Method 5: Using itertools.zip_longest Function

For lists of unequal length, where you want to pair elements and use a fill value for shorter lists, the itertools.zip_longest function from the itertools module can be used.

Here’s an example:

from itertools import zip_longest

list_a = [13, 14]
list_b = ['m', 'n', 'o']
list_of_tuples = list(zip_longest(list_a, list_b, fillvalue='No Match'))
print(list_of_tuples)

This code uses zip_longest, providing a fillvalue for elements in list_b without a corresponding element in list_a. It’s ideal for zipping lists of different lengths.

Summary/Discussion

  • Method 1: Using the Zip Function. Strengths: Simple and idiomatic. Weaknesses: Doesn’t handle lists of different lengths.
  • Method 2: List Comprehension with Zip. Strengths: Compact and Pythonic. Weaknesses: Less readable to newcomers, doesn’t handle different length lists.
  • Method 3: Using a For Loop. Strengths: Explicit and easy to understand for beginners. Weaknesses: More verbose and potentially less efficient.
  • Method 4: Using the map() and lambda Functions. Strengths: Functional programming paradigm, concise. Weaknesses: Can be less intuitive for those unfamiliar with lambdas or map.
  • Method 5: Using itertools.zip_longest Function. Strengths: Handles lists of different lengths with a fill value. Weaknesses: Requires importing a module, slightly less known method.

If you want to keep learning Python, check out the cheat sheet for free here: