If the list of tuples is sorted, itertools.groupby
, as suggested by @gnibbler, is not a bad alternative to defaultdict
, but it needs to be used differently than he suggested:
import itertools
import operator
def lot_to_dict(lot):
key = operator.itemgetter(0)
# if lot's not sorted, you also need...:
# lot = sorted(lot, key=key)
# NOT in-place lot.sort to avoid changing it!
grob = itertools.groupby(lot, key)
return dict((k, [v[1] for v in itr]) for k, itr in grob)
For “merging” dicts of lists into a new d.o.l…:
def merge_dols(dol1, dol2):
keys = set(dol1).union(dol2)
no = []
return dict((k, dol1.get(k, no) + dol2.get(k, no)) for k in keys)
I’m giving []
a nickname no
to avoid uselessly constructing a lot of empty lists, given that performance is important. If the sets of the dols’ keys overlap only modestly, faster would be:
def merge_dols(dol1, dol2):
result = dict(dol1, **dol2)
result.update((k, dol1[k] + dol2[k])
for k in set(dol1).intersection(dol2))
return result
since this uses list-catenation only for overlapping keys — so, if those are few, it will be faster.