There are many ways to do this task
first using detaultdict :
import collections data=[(9600002, 42, 3), (9600001, 17, 3), (9600003, 11, 1), (9600002, 14, 5), (9600001, 17, 1), (9600003, 11, 4), (9600001, 17, 4), (9600001, 14, 3), (9600002, 42, 6), (9600002, 42, 1)] d=collections.defaultdict(list) for i in data: d[(i[0],i[1])].append(i) print(list(filter(lambda x:len(x)>1,d.values())))
output:
[[(9600003, 11, 1), (9600003, 11, 4)], [(9600001, 17, 3), (9600001, 17, 1), (9600001, 17, 4)], [(9600002, 42, 3), (9600002, 42, 6), (9600002, 42, 1)]]
Second using itertools groupby :
import itertools print(list(filter(lambda x:len(x)>1,[list(j) for i,j in itertools.groupby(sorted(data),key=lambda x:(x[0],x[1]))])))
output:
[[(9600001, 17, 1), (9600001, 17, 3), (9600001, 17, 4)], [(9600002, 42, 1), (9600002, 42, 3), (9600002, 42, 6)], [(9600003, 11, 1), (9600003, 11, 4)]]
Third approach
At last you can also try manual approach instead of using any import :
d={} for i in data: if (i[0],i[1]) not in d: d[(i[0],i[1])]=[i] else: d[(i[0],i[1])].append(i) print(list(filter(lambda x:len(x)>1,d.values())))
output:
[[(9600003, 11, 1), (9600003, 11, 4)], [(9600001, 17, 3), (9600001, 17, 1), (9600001, 17, 4)], [(9600002, 42, 3), (9600002, 42, 6), (9600002, 42, 1)]]