Sometimes, we want to get all combinations of a list of lists with Python.
In this article, we’ll look at how to get all combinations of a list of lists with Python.
How to get all combinations of a list of lists with Python?
to get all combinations of a list of lists with Python, we can use the itertools.product
method.
For instance, we write
import itertools
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
p = list(itertools.product(*a))
to call itertools.product
with the lists in a
as arguments.
This will return an iterable with the cartesian product of the lists.
And then we call list
to convert the iterable to a list.
Conclusion
to get all combinations of a list of lists with Python, we can use the itertools.product
method.