You can use groupby
from itertools
and take the key:
groupby
objects yield key-group pairs where the group is a generator.
It can Group consecutive items together
import itertools
input_list = [[1,2], [3,4,5], [1,2], [3,4], [3,4,5]]
input_list.sort()
res_list = list(item for item,_ in itertools.groupby(input_list))
def print_groupby(iterable):
for k, g in itertools.groupby(iterable):
print("key: '{}'--> group: {}".format(k, list(g)))
print_groupby(input_list)
print('result is:', res_list)
Outputs:
key: '[1, 2]'--> group: [[1, 2], [1, 2]]
key: '[3, 4]'--> group: [[3, 4]]
key: '[3, 4, 5]'--> group: [[3, 4, 5], [3, 4, 5]]
result is: [[1, 2], [3, 4], [3, 4, 5]]