# Invert a dictionary that can have several keys mapping to the same value # In the inverse dictionary every value is mapped to the list of its keys D={'a':1, 'b':1, 'c':2, 'd':2, 'e':3} D_inv={} for k,v in D.items(): D_inv[v]=D_inv.get(v,[])+[k]