How to generate all combination from values in dict of lists in Python

If you want to keep the key:value in the permutations you can use:

import itertools
keys, values = zip(*my_dict.items())
permutations_dicts = [dict(zip(keys, v)) for v in itertools.product(*values)]

this will provide you a list of dicts with the permutations:

print(permutations_dicts)
[{'A':'D', 'B':'F', 'C':'I'}, 
 {'A':'D', 'B':'F', 'C':'J'},
 ...
 ]

Disclaimer: not exactly what the OP was asking, but google send me here looking for that. If you want to obtain what OP is asking you should just remove the dict part in the list comprehension, i.e.

permutations_dicts = [v for v in itertools.product(*values)]

Leave a Comment