iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐍

Calculating the average of a list of dictionaries in Python

に公開

Calculating the average of a list of dictionaries by key is quite a hassle, so I wrote a simple function to handle it.

def dict_average(dicts: Iterable[Dict]) -> Dict:
    dicts: List[Dict] = list(dicts)
    averaged = {}

    for k, v in dicts[0].items():
        try:
            v = v.item()
        except:
            pass
        if type(v) in [int, float]:
            averaged[k] = v / len(dicts)
        else:
            averaged[k] = [v]

    for d in dicts[1:]:
        for k, v in d.items():
            try:
                v = v.item()
            except:
                pass
            if type(v) in [int, float]:
                averaged[k] += v / len(dicts)
            else:
                averaged[k].append(v)

    return averaged

For the values of each dictionary object, the function is designed to calculate the average for Python int and float types per key, while other types are collected into a list.

As a point of caution, numeric types from NumPy or PyTorch are not Python primitives, so you cannot calculate the numeric average if you branch logic based strictly on whether the type is int or float.
To address this, since NumPy and PyTorch numeric types can be converted to Python primitives using the .item() method, I use a try block to convert them into Python primitives.

Here is an example of how it works.

dict_list = [
	{"a": 3, "b": "hoge"},
	{"a": 2, "b": "fuga"},
]

print(dict_average(dict_list))

# {
#	'a': 2.5,
#	'b': ['hoge', 'fuga'],
# }

If you have any suggestions for improvement, please let me know in the comments!

Discussion