3 ms·
Perhaps not one line, maybe not perfect Python but definitely O(n): groupby_dict = collections.defaultdict(int) for l in mylist: groupby_dict[l
by rstuart4133 5y ago
Perhaps not one line, maybe not perfect Python but definitely O(n):
groupby_dict = collections.defaultdict(int)
for l in mylist:
groupby_dict[l['thing']] += l['count']
newlist = []
for thing, count in groupby_dict.items():
newlist.append(dict(thing=thing, count=count))
Knowing how do that does require the deep knowledge of data structures I'd expect every professional programmer to have, and doing a quick scan of the languages standard library (maybe an hours work) to see what it offers. After that you're set for solving not only this problem, but almost guaranteed to solve all problems you are likely to hit pretty optimally in Python. I have no idea where Stack Overflow fits into the picture.
If you go a step further and do the Python standard tutorial found in the docs, you would discover how a finger weary experienced Python programmer might write those last three lines:
newlist = [dict(thing=thing, count=count) for thing, count in groupby_dict.items()]
Comprehensions are sweet Python syntactic sugar, but unlike mapping your existing encyclopaedic knowledge of data structures onto the languages standard library they aren't necessary for a casual user of the language. Indeed, some Python'istas will tell you resisting such delights make for clearer code.
But trusting a quick Stack Overflow to tell you the optimal way to use a languages data structures - you must be kidding me.
- qsort 5y agoDoesn't detract from any of your points, just as a FYI: Python has a counter data structure in the standard library. The "pythonic" way would be something like this: groupby_dict = Counter() for r in response: groupby_dict.update({r['thing']: r['count']}) Counter is a subclass of dict, no further conversion needed.
- ec109685 5y agoDeep knowledge is overstating it I think. Knowing a few data structures and their Big O notation for various operations goes a long way. You don’t need to exactly know how it is implemented under the covers. SQL is similar that once you grasp what data structure it uses to store your data, you can intuit what should be fast and what is slow.