Masih
Masih

Reputation: 980

Removing repetitive lists in a list of list

I have a list of lists and i want to remove repetitive items in the list.

my_list=
[[[2, 5, 71.1], [1, 3, 70.0]],
[[2, 5, 71.1], [1, 3, 70.0]],
[[2, 5, 71.1], [1, 3, 70.0]],
[[5, 10, 80.0], [6, 9, 80.0], [5, 8, 80.0]],
[[5, 10, 80.0], [6, 9, 80.0], [5, 8, 80.0]],
[[5, 10, 80.0], [6, 9, 80.0], [5, 8, 80.0]],
[[10, 12, 80.0]],
[[14, 16, 80.0], [13, 20, 81.0]],
[[14, 16, 80.0], [13, 20, 81.0]],
[[22, 24, 80.0]],
[[26, 28, 80.0], [25, 40, 80.0]],
[[26, 28, 80.0], [25, 40, 80.0]],
[[40, 42, 80.0], [40, 41, 80.0]],
[[40, 42, 80.0], [40, 41, 80.0]],
[[44, 45, 80.0]]]

output_wanted=
 [[[2, 5, 71.1], [1, 3, 70.0]],
 [[5, 10, 80.0], [6, 9, 80.0], [5, 8, 80.0]],
 [[10, 12, 80.0]],
 [[14, 16, 80.0], [13, 20, 81.0]],
 [[22, 24, 80.0]],
 [[26, 28, 80.0], [25, 40, 80.0]],
 [[40, 42, 80.0], [40, 41, 80.0]],
 [[44, 45, 80.0]]]

i need something like the set function which does the job fast. but i can not use the set function in this case. is there any way for doing it?

Upvotes: 1

Views: 97

Answers (1)

xiº
xiº

Reputation: 4687

This is because list is not hashable. You need to convert the elements of your list in tuples (which are hashable) before applying set().

>>> my_list = [[1, 2], [1, 2], [3, 4]]
>>> result = [list(el) for el in set(tuple(el) for el in my_list)]
[[1, 2], [3, 4]]

Updated with your new data:

>>> [list(list(y) for y in el) 
        for el in set([tuple(tuple(x) for x in el) for el in my_list])]

[[[26, 28, 80.0], [25, 40, 80.0]],
 [[10, 12, 80.0]],
 [[40, 42, 80.0], [40, 41, 80.0]],
 [[44, 45, 80.0]],
 [[5, 10, 80.0], [6, 9, 80.0], [5, 8, 80.0]],
 [[22, 24, 80.0]],
 [[14, 16, 80.0], [13, 20, 81.0]],
 [[2, 5, 71.1], [1, 3, 70.0]]]

Upvotes: 1

Related Questions