I was checking the code of the toolz library's groupby function in Python and I found this:
def groupby(key, seq):
""" Group a collection by a key function
"""
if not callable(key):
key = getter(key)
d = collections.defaultdict(lambda: [].append)
for item in seq:
d[key(item)](item)
rv = {}
for k, v in d.items():
rv[k] = v.__self__
return rv
Is there any reason to use rv[k] = v.__self__ instead of rv[k] = v?



dis a mapping of key to theappendmethod of the lists created by the lambda expression, sorv[k] = v.__self__is building a mapping of key to the actual list. There's context on why this somewhat baffling implementation was used (TL;DR: speed) here.