This question already has an answer here:
I have dict that describes possible config values, e.g.
{'a':[1,2], 'b':[3,4,5]}
I want to generate list of all acceptable configs, e.g.
[{'a':1, 'b':3},
{'a':1, 'b':4},
{'a':1, 'b':5},
{'a':2, 'b':3},
{'a':2, 'b':4},
{'a':1, 'b':5}]
I've looked through the docs and SO and it certainly seems to involve itertools.product
, but I can't get it without a nested for loop.
You can also try this:
You don't need a nested
for
loop here:product(*d.values())
produces your required value combinations, anddict(zip(d.keys(), combo))
recombines each combination with the keys again.Demo: