I have a very large collection of (p, q) tuples that I would like to convert into a dictionary of lists where the first item in each tuple is a key that indexes a list that
collections.defaultdict works like this:
from collections import defaultdict dic = defaultdict(list) for i, j in tuples: dic[i].append(j)
similar for the dicts:
a, b = {1:[2, 3], 2:[3]}, {1:[4], 3:[1]} de = defaultdict(list, a) for i, j in b.items(): de[i].extend(j)