Amjad Syed Amjad Syed - 5 months ago 3x
Python Question

remove duplicate elements from list in python

i'm new to programming and i'm trying to remove duplicates from list in python. However i'm unable to perform it using set(). List contains IP address and date following is my code and list

l = [['', '2015-08-29'], ['', '2015-08-29'], ['', '2015-08-29'], ['', '2015-08-29'], ['', '2015-08-29'], ['', '2015-08-29'],['', '2016-03-12'], ['', '2015-05-02'], ['', '2016-03-12'], ['', '2016-03-12'], ['', '2015-04-25'], ['', '2015-04-25'], ['', '2016-03-12'], ['', '2015-04-25'], ['', '2015-04-25'], ['', '2015-11-28'], ['', '2015-11-28'], ['', '2015-08-29'], ['', '2015-04-25'], ['', '2015-04-25']]

fl = set(l)
print fl

I get the following error:

Traceback (most recent call last):
File "C:/Users/syeam02.TANT-A01/PycharmProjects/security/", line 18, in <module>
fl = set(array)
TypeError: unhashable type: 'list'

Thanks in advance.


You can't use list type elements in a set, since list is mutable entity. For the same reason, you can't use list as key of a dictionary. You need to have an immutable type, like tuple.

So, you can convert inner elements to tuple before passing to set:

set(tuple(li) for li in l)

Check this section to doc:

Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.