Create cartesian product, equivalent to a nested for-loop, combinations of the items dict. Args: items: dict of items to be combined. Returns: list: list of dictionaries with the combinations of the input items. Example: >>> dict_product(x=[1, 2], y=[3, 4])
(**items: Iterable[Any])
| 914 | |
| 915 | |
| 916 | def dict_product(**items: Iterable[Any]) -> list[dict]: |
| 917 | """Create cartesian product, equivalent to a nested for-loop, combinations of the items dict. |
| 918 | |
| 919 | Args: |
| 920 | items: dict of items to be combined. |
| 921 | |
| 922 | Returns: |
| 923 | list: list of dictionaries with the combinations of the input items. |
| 924 | |
| 925 | Example: |
| 926 | >>> dict_product(x=[1, 2], y=[3, 4]) |
| 927 | [{'x': 1, 'y': 3}, {'x': 1, 'y': 4}, {'x': 2, 'y': 3}, {'x': 2, 'y': 4}] |
| 928 | """ |
| 929 | keys = items.keys() |
| 930 | values = items.values() |
| 931 | prod_values = product(*values) |
| 932 | prod_dict = [dict(zip(keys, v)) for v in prod_values] |
| 933 | return prod_dict |
| 934 | |
| 935 | |
| 936 | if __name__ == "__main__": |
no outgoing calls
no test coverage detected
searching dependent graphs…