Skip to content

dicts

combine_dicts(*dicts)

Combines multiple dictionaries into a single dictionary joining the values as a list for each key.

Parameters:

Name Type Description Default
*dicts Any

the dictionaries to be combined.

()

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: the combined dictionary.

Source code in src/distilabel/utils/dicts.py
def combine_dicts(*dicts: Any) -> Dict[str, Any]:
    """Combines multiple dictionaries into a single dictionary joining the values
    as a list for each key.

    Args:
        *dicts (Any): the dictionaries to be combined.

    Returns:
        Dict[str, Any]: the combined dictionary.
    """
    combined_dict = defaultdict(list)
    for d in dicts:
        for key, value in d.items():
            combined_dict[key].append(value)
    return dict(combined_dict)