Menu Close

How to create permutations between two lists of unequal length with Python?

Sometimes, we want to create permutations between two lists of unequal length with Python.

In this article, we’ll look at how to create permutations between two lists of unequal length with Python.

How to create permutations between two lists of unequal length with Python?

To create permutations between two lists of unequal length with Python, we can use the itertools.product method.

For instance, we write

a = ["foo", "melon"]
b = [True, False]
c = list(itertools.product(a, b))

to call itertools.product with a and b to return an iterator with the tuples with the different permutations of items between a and b.

Then we convert that to a list with list.

Conclusion

To create permutations between two lists of unequal length with Python, we can use the itertools.product method.

Posted in Python, Python Answers