Skip to content
Advertisement

How to optimise the grouping of values in lists by key in a dictionary list?

The script below works but I was wondering if there is a faster solution? With very large dictionary lists I noticed a small delay.

from collections import defaultdict

input = [{"first": 1.56,
          "second": [1, 2, 3, 4]}, {"first": 7.786,
                                    "second": [5, 6, 7, 8]}, {"first": 4.4,
                                                              "second": [9, 10, 11, 12]}]
output = [{"first": [1.56, 7.786, 4.4],
          "second":[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]}]

my_dictionary = defaultdict(list)
for item in input:
    for key, value in item.items():
        my_dictionary[key].append(value)
print(my_dictionary)

#defaultdict(<class 'list'>, {'first': [1.56, 7.786, 4.4], 'second': [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]})

Advertisement

Answer

It seems keys in the dictionaries are the same, so you could use a dict comprehension:

out = {k:[d[k] for d in input] for k in input[0]}

Another pretty fast alternative is to use the cytoolz module.

# pip install cytoolz
from cytoolz.dicttoolz import merge_with
out = merge_with(list, *input)

Output:

{'first': [1.56, 7.786, 4.4],
 'second': [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]}

Timings:

>>> my_input = input * 10000
>>> %%timeit
... my_dictionary = defaultdict(list)
... for item in my_input:
...     for key, value in item.items():
...         my_dictionary[key].append(value)
20.3 ms ± 2.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

>>> %timeit out = {k:[d[k] for d in my_input] for k in my_input[0]}
4.65 ms ± 541 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

>>> %timeit out = merge_with(list, *my_input)
5.58 ms ± 2.09 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement