1

I am trying to get unique lists which can consist duplicate values,can I use sets to get unique lists?

To be more specific ,here is an example:

my_list=[[1,2,1],[1,2,2],[1,2,2],[1,1,2],[2,2,2]]

what i would want is :

set_list=[[1,2,1],[1,2,2],[1,1,2],[2,2,2]]

is this possible? Thanks in advance for your kind response :)

deb
  • 39
  • 4

2 Answers2

2

Lists are not hashable, but you can have a set of tuples, :

set(map(tuple, my_list))
# {(1, 1, 2), (1, 2, 1), (1, 2, 2), (2, 2, 2)}
Psidom
  • 171,477
  • 20
  • 249
  • 286
2

No, a list is not hashable. You will get the following error:

TypeError: unhashable type: 'list'

Given the list only contains hashable objects, you can however convert the list to a tuple and add the tuples. So you could do something like:

>>> my_list=[[1,2,1],[1,2,2],[1,2,2],[1,1,2],[2,2,2]]
>>> set_tuples = {tuple(a_list) for a_list in my_list}
>>> set_tuples
{(1, 2, 2), (1, 2, 1), (2, 2, 2), (1, 1, 2)}

You can then for instance construct a uniqueness filter with:

my_list=[[1,2,1],[1,2,2],[1,2,2],[1,1,2],[2,2,2]]
result = []
unique_set = set()
for sublist in my_list:
    the_tuple = tuple(sublist)
    if the_tuple not in unique_set:
        unique_set.add(the_tuple)
        result.append(sublist)

So all operations on the set are done with tuples. This gives:

>>> result
[[1, 2, 1], [1, 2, 2], [1, 1, 2], [2, 2, 2]]
Willem Van Onsem
  • 321,217
  • 26
  • 295
  • 405
  • Well I am still not getting the desired result :/ I got the below result : for i in my_list: ... the_tuple=tuple(i) ... if the_tuple not in unique_set: ... unique_set.add(the_tuple) ... result.append(i) ... >>> unique_set set([((),), ((1,), (2,), (2,)), ((1, 2), (1, 2), (2, 1), (2, 2), (2, 1), (2, 2))]) >>> result [[(1,), (2,), (2,)], [(1, 2), (1, 2), (2, 1), (2, 2), (2, 1), (2, 2)]] >>> – deb Mar 10 '17 at 16:26
  • @deb: mind that `my_list` contains **all lists**. – Willem Van Onsem Mar 10 '17 at 16:28
  • what i mean is it is still showing duplicate lists in the same – deb Mar 10 '17 at 16:29
  • @deb: if you run the code last code fragment (which is self-containig), you get the reported `result`... You probably have a `for` too much since your unique set seems to construct single element tuples, two-element tuples, etc. – Willem Van Onsem Mar 10 '17 at 16:30
  • do you mean apply this to each tuple? that is first to the_tuple[0]and so on..? – deb Mar 10 '17 at 16:34
  • @deb: your question is "filtering out duplicates". The code fragment below shows you how to get a `result` without duplicates. Why do you modify it? – Willem Van Onsem Mar 10 '17 at 16:35
  • got your point(y) , was lil silly of me... :D,,, btw thanks @Willem – deb Mar 10 '17 at 16:37