In Python, we can get all combinations of two lists easily. The easiest way to obtain all combinations of two lists is with list comprehension.

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

combinations = [(x,y) for x in list1 for y in list2]

print(combinations)

#Output:
[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1), ('c', 2), ('c', 3)]

You can also use a for loop to get all combinations of two list objects in Python.

list1 = ["a", "b"]
list2 = [1, 2]

def combinations(lst1, lst2):
    result = []
    for x in lst1:
        for y in lst2:
            result.append((x,y))
    return result

print(combinations(list1,list2))

#Output:
[('a', 1), ('a', 2), ('b', 1), ('b', 2)]

Finally, the Python itertools module has a function product() which finds the Cartesian product, or all combinations, for you.

from itertools import product

list1 = ["a", "b"]
list2 = [1, 2]

print(list(product(list1,list2)))

#Output:
[('a', 1), ('a', 2), ('b', 1), ('b', 2)]

When working with collections of data in Python, the ability to manipulate them and create new collections is very valuable.

One such manipulation is the ability to get all combinations of two lists in a new list.

All combinations of two sets A and B is the Cartesian Product of these two sets.

The Cartesian Product of two sets A and B is the set of all possible ordered pairs (a, b), where a is in A and b is in B. We can get the Cartesian product between two lists easily with Python.

The easiest way to get the Cartesian product and all of the combinations of two lists is with list comprehension.

Below is a simple example of how to get all combinations of two lists in Python using list comprehension.

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

combinations = [(x,y) for x in list1 for y in list2]

print(combinations)

#Output:
[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1), ('c', 2), ('c', 3)]

Using for Loop to Get Combinations of Two Lists in Python

We can also use a loop to get the different combinations of lists in Python.

We can define a loop easily which will loop over all possible combinations of our lists and create ordered pairs in the form of tuples.

Below is a simple example of how to get combinations of two lists in Python using iteration.

list1 = ["a", "b"]
list2 = [1, 2]

def combinations(lst1, lst2):
    result = []
    for x in lst1:
        for y in lst2:
            result.append((x,y))
    return result

print(combinations(list1,list2))

#Output:
[('a', 1), ('a', 2), ('b', 1), ('b', 2)]

Using itertools product() Function to Get Combinations of Two Lists in Python

The itertools module has many great functions which allow us to iterate over collections and perform complex tasks easily.

We can use the itertools product() function to get the Cartesian product of lists.

To get the all combinations of two lists in Python using product(), just pass the lists to the function.

Below is a simple example of how to get all combinations of two lists in Python using itertools and product().

from itertools import product

list1 = ["a", "b"]
list2 = [1, 2]

print(list(product(list1,list2)))

#Output:
[('a', 1), ('a', 2), ('b', 1), ('b', 2)]

Hopefully this article has been useful for you to learn how to get combinations of lists in Python.

Categorized in:

Python,

Last Update: March 11, 2024