from collections import defaultdict
l = [(1, 'a', 22), (2, 'b', 56), (1, 'b', 34), (2, 'c', 78), (3, 'd', 47)]
d = defaultdict(dict)
for x, y, z in l:
    d[x][y] = z
How to convert a list of tuples to a dictionary of dictionaries in Python?