lst = [(1, 'a', 22), (2, 'b', 56), (1, 'b', 34), (2, 'c', 78), (3, 'd', 47)]
{key: {v[1]: v[2] for v in vals} for key, vals in itertools.groupby(sorted(
    lst), key=operator.itemgetter(0))}
How to convert a list of tuples to a dictionary of dictionaries in Python?