Alan Isaac <aisaac at american.edu> writes: > print "method 2: groupby" > t=time.clock() > d = dict() > kv_sorted = sorted(kv, key=lambda x: id(x[0])) How about something like: kv_sorted = sorted(kv, key=lambda x: (id(x[0]), x[1])) Now do your groupby and the first element of each group is the minimum for that group.