Explode a dict - Get all combinations of the values in a dictionary

MSeifert

I want to get all combinations of the values in a dictionary as multiple dictionaries (each containing every key of the original but only one value of the original values). Say I want to parametrize a function call with:

kwargs = {'a': [1, 2, 3], 'b': [1, 2, 3]}

How do I get a list of all the combinations like this:

combinations = [{'a': 1, 'b': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 3},
                {'a': 2, 'b': 1}, {'a': 2, 'b': 2}, {'a': 2, 'b': 3},
                {'a': 3, 'b': 1}, {'a': 3, 'b': 2}, {'a': 3, 'b': 3}]

There can be an arbitary amount of keys in the original kwargs and each value is garantueed to be an iterable but the number of values is not fixed.

If possible: the final combinations should be a generator (not a list).

thefourtheye

You can flatten the kwargs to something like this

>>> kwargs = {'a': [1, 2, 3], 'b': [1, 2, 3]}
>>> flat = [[(k, v) for v in vs] for k, vs in kwargs.items()]
>>> flat
[[('b', 1), ('b', 2), ('b', 3)], [('a', 1), ('a', 2), ('a', 3)]]

Then, you can use itertools.product like this

>>> from itertools import product
>>> [dict(items) for items in product(*flat)]
[{'a': 1, 'b': 1},
 {'a': 2, 'b': 1},
 {'a': 3, 'b': 1},
 {'a': 1, 'b': 2},
 {'a': 2, 'b': 2},
 {'a': 3, 'b': 2},
 {'a': 1, 'b': 3},
 {'a': 2, 'b': 3},
 {'a': 3, 'b': 3}]

itertools.product actually returns an iterator. So you can get the values on demand and build your dictionaries. Or you can use map, which also returns an iterator.

>>> for item in map(dict, product(*flat)):
...     print(item)
...
...
{'b': 1, 'a': 1}
{'b': 1, 'a': 2}
{'b': 1, 'a': 3}
{'b': 2, 'a': 1}
{'b': 2, 'a': 2}
{'b': 2, 'a': 3}
{'b': 3, 'a': 1}
{'b': 3, 'a': 2}
{'b': 3, 'a': 3}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Split python dictionary to result in all combinations of values

How to get all combinations from a given dictionary?

Trying to get all values in a dict

Get all combinations from list values

Get all uniq combinations of values with respect to groups

How to get the product of all combinations of values by column?

Format a string with all combinations of multiple dictionary key / values

get all values from dictionary

pandas explode rows with all substring combinations

How to get all possible combinations to make a string with a dictionary

Get all possible combinations of array values when repetitive values are possible

Get a dict of all variables currently in scope and their values

Python dict with lists as values, create list of dicts with all possible combinations of original keys-values

Generate all combinations of values

Finding the combinations of values in a dictionary of hashsets

All combinations from dictionary of lists

Is it possible to get selective combinations of dict in a dict?

Python pandas dataframe get all combinations of column values?

Is there an optimal way to get all combinations of values in a grouped pandas dataframe?

Get all possible combinations of 3 values from n possible elements

Get all combinations of a variable and their corresponding values in a grouped data set

How to get all unique combinations of values in one column that are in another column

How to get all possible combinations from hash's values

kql - get all values from a dictionary

python get all values from nested dictionary

How to generate all possible combinations in a dictionary in a dictionary

Get all combinations of a type

Get all combinations

Get all combinations of an array