Python: Group and aggregate list of dictionaries [without Counter]

948 views Asked by At

I have a problem exactly like described in this thread: Group by and aggregate the values of a list of dictionaries in Python

The given solution works perfectly but doesn't work on anything lower than Python 2.7 and I'm trying to maintain compatibility with 2.6.6.

My data looks like so (stolen from linked thread above):

my_dataset = [  
    {
        'type': 'type1',
        'value1': 10
    },
    {
        'type': 'type2',
        'value2': 10
    },
    {
        'type': 'type1,
        'value1': 10
    }
]

And this is what I want it to return:

[
    {
        'type': 'type1',
        'value1': 20
    },
    {
        'type': 'type2',
        'value1': 10
    }
]

What is the most efficient way of doing this without using Counter?

UPDATE

Aprillion commented below and directed me to backport_collections which looks like it should be exactly what I need but I'm still getting errors on my 2.6.6 builds.

The function looks like this:

from backport_collections import defaultdict, Counter
def group_and_sum_dataset(dataset, group_by_key, sum_value_keys, sort_by_key):

    container = defaultdict(Counter)

    for item in dataset:
        key = item[group_by_key]
        values = {k: item[k] for k in sum_value_keys}
        container[key].update(values)

    new_dataset = [
        dict([(group_by_key, item[0])] + item[1].items())
        for item in container.items()
    ]
    new_dataset.sort(key=lambda item: item[sort_by_key], reverse=True)

    return new_dataset

When I try to run it I get a syntax error:

values = {k: item[k] for k in sum_value_keys}
                       ^
SyntaxError: invalid syntax

Still runs fine on 2.7. Not sure this is even Counter related now.

1

There are 1 answers

1
dting On

The problem you are having is that you are trying to do a dictionary comprehension which wasn't added until python 2.7.

values = dict((k, item[k]) for k in sum_value_keys)