Skip to content

Instantly share code, notes, and snippets.

@searope
Created May 17, 2022 14:41
Show Gist options
  • Select an option

  • Save searope/32e122166c3290ba108c91402961ecf8 to your computer and use it in GitHub Desktop.

Select an option

Save searope/32e122166c3290ba108c91402961ecf8 to your computer and use it in GitHub Desktop.
permutates all elements in list values of a dictionary to return a dictionary with the same keys but single values
import itertools
my_dict = {'A':['D','E'],'B':['F','G','H'],'C':['I','J']}
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'},
# ...
#]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment