Home > Enterprise >  Extract all possible combinations of unique elements in dict of lists
Extract all possible combinations of unique elements in dict of lists

Time:11-13

I have this input:

d = {'a': ['A', 'B', 'C'], 'b': ['A', 'B', 'C'], 'c': ['D', 'E'], 'd': ['E', 'F', 'G']}

How can I extract all the possible unique samplings per list? One of the possible output is for example:

d = {'a': 'A', 'b': 'B', 'c': 'D', 'd': 'E'}

or

d = {'a': 'B', 'b': 'A', 'c': 'E', 'd': 'F'}

and so on..
Any idea? Thank you

CodePudding user response:

This is what you are looking for

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