Converting a list of tuples into a dict

Solution 1:

l = [
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]

d = {}
for x, y in l:
    d.setdefault(x, []).append(y)
print d

produces:

{'a': [1, 2, 3], 'c': [1], 'b': [1, 2]}

Solution 2:

Slightly simpler...

from collections import defaultdict

fq = defaultdict(list)
for n, v in myList:
    fq[n].append(v)
    
print(fq) # defaultdict(<type 'list'>, {'a': [1, 2, 3], 'c': [1], 'b': [1, 2]})