top | item 29660876

(no title)

Rainymood | 4 years ago

This solution is really bad, read it in as a dataframe and use a groupby...

discuss

order

halfdan|4 years ago

That's even worse. You don't need to throw in a massive library like pandas, especially not when OP is build a basic API. Sure, if you're in a data science-y project where pandas is already a dependency, go nuts.

rbanffy|4 years ago

It really depends on the size of the list you want to process. If it's 10 items, pandas is overkill (and probably slower). If it's a million items, pandas is a great solution.

I have a nagging feeling there is an easier way to do this, but my quick and dirty solution was

    def merge_list1(l):
    other_dict = defaultdict(lambda: 0)
        for t, c in ((i['thing'], i['count']) for i in l):
            other_dict[t] += c
        return ({'thing': k, 'count': other_dict[k]} for k in other_dict)
which is still readable, but probably far from optimal.