from collections import defaultdict d=defaultdict(set) s=[("001","A"),("001","C"),("002","B")] for k,v in s: d[k].add(v) print(sorted(d.items()))
from collections import defaultdict d=defaultdict(set) s=[("001","A"),("001","C"),("002","B")] for k,v in s: d[k].add(v) print(sorted(d.items()))