tr = [[1, 2, 3], [1, 2, 4], [5, 6, 7]]
{a[0]: {b[1]: {c[2] for c in [y for y in tr if y[1] == b[1]]} for b in [x for
    x in tr if x[0] == a[0]]} for a in tr}
How to turn a list into nested dict in Python