Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Created August 12, 2017 01:52
Show Gist options
  • Save hughdbrown/2e6752ab64409cd85ce24199f40b8d89 to your computer and use it in GitHub Desktop.
Save hughdbrown/2e6752ab64409cd85ce24199f40b8d89 to your computer and use it in GitHub Desktop.
Make a list of dictionaries from a dict of lists
>>> parameters3 = {
... 'g': [100],
... 'h': [200, 201],
... 'i': [300, 301, 302],
... }
>>>
>>> from itertools import product
>>> def _exhaustive_list(parameters):
... keys, values = list(parameters.keys()), list(parameters.values())
... return [
... dict(zip(keys, comb))
... for comb in product(*values)
... ]
...
>>> from pprint import pprint
>>> pprint(_exhaustive_list(parameters3))
[{'g': 100, 'h': 200, 'i': 300},
{'g': 100, 'h': 201, 'i': 300},
{'g': 100, 'h': 200, 'i': 301},
{'g': 100, 'h': 201, 'i': 301},
{'g': 100, 'h': 200, 'i': 302},
{'g': 100, 'h': 201, 'i': 302}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment