How to turn a list of iterators into all possible lists using those iterators
Question:
I have a list of iterators, such as l = [range(1), range(2), range(3)]
and each iterator iterates over the possibilities that could be in that slot.
However, I don’t know how long the list is going to be.
In this case, to get what I want, I can easily do [[val1, val2, val3] for val1 in l[0] for val2 in l[1] for val3 in l[2]]
.
I’ve been looking a lot at itertools, and couldn’t see anything that matched this pattern.
What function can take in a list of iterators and generate a list of all the possibilities for lists where each value in the list is a value of one of the generators in the slots?
Answers:
As I was typing up this question, I realized that there is in fact an itertools function that accomplishes this.
list(itertools.product(*l))
does the trick.
I figure I’ll just post this as a self-answered question in case someone else has similar terminology.
I have a list of iterators, such as l = [range(1), range(2), range(3)]
and each iterator iterates over the possibilities that could be in that slot.
However, I don’t know how long the list is going to be.
In this case, to get what I want, I can easily do [[val1, val2, val3] for val1 in l[0] for val2 in l[1] for val3 in l[2]]
.
I’ve been looking a lot at itertools, and couldn’t see anything that matched this pattern.
What function can take in a list of iterators and generate a list of all the possibilities for lists where each value in the list is a value of one of the generators in the slots?
As I was typing up this question, I realized that there is in fact an itertools function that accomplishes this.
list(itertools.product(*l))
does the trick.
I figure I’ll just post this as a self-answered question in case someone else has similar terminology.