-1

I have a big list of lists with different sizes. How to efficiently create a new list containing only lists with unique combination of numbers?

input_list = [[1,2], [3,4,5], [3,4], [3,4,5]]
result = [[1,2], [3,4,5], [3,4]]

Elements in lists are ordered.

Oleg Dats
  • 3,933
  • 9
  • 38
  • 61

1 Answers1

1

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]]
David Meu
  • 1,527
  • 9
  • 14