I have a python dictionary which includes some nested lists and might look as follows:
Dict = {
"a": [0.1, 20],
"b": 300,
"c": ["sun", "moon"],
"d": "water",
}
I am now trying to generate all possible unique permutations of this dictionary where each key only has one entry (i.e. no nested lists anymore). The desired dictionaries would look like these:
Dict1 = {
"a": 0.1,
"b": 300,
"c": "sun",
"d": "water",
}
Dict2 = {
"a": 0.1,
"b": 300,
"c": "moon",
"d": "water",
}
Dict3 = {
"a": 20,
"b": 300,
"c": "sun",
"d": "water",
}
Dict4 = {
"a": 20,
"b": 300,
"c": "moon",
"d": "water",
}
After looking at comparable problems I have tried my luck with itertools but have not succeeded thus far. I am rather new to pyhton and am also not sure if dictionaries are even the most appropriate datastructure for this problem, so any advice will me much appreciated.
import itertools
keys, values = zip(*Dict.items())
results = [dict(zip(keys, v)) for v in itertools.product(values)]