Created
May 17, 2022 14:41
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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