pytoolz / toolz

A functional standard library for Python.
http://toolz.readthedocs.org/
Other
4.66k stars 259 forks source link

Use defaultdict for collecting intermediate values in merge_with #532

Closed groutr closed 2 years ago

groutr commented 2 years ago

This approach also avoids calling factory() twice. It combines the ideas of itertoolz.groupby and cytoolz.merge_with.

Best of all: there is a small performance gain:

In [2]: rand_d = [dict(zip(range(50), range(50))) for i in range(100)]

In [12]: %timeit merge_with(max, rand_d)
527 µs ± 14.4 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [13]: %timeit toolz.merge_with(max, rand_d)
730 µs ± 24.2 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
groutr commented 2 years ago

ping @eriknw

eriknw commented 2 years ago

LGTM, thanks @groutr!