0

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 = [['10.136.161.80', '2015-08-29'], ['10.136.161.80', '2015-08-29'], ['10.136.161.80', '2015-08-29'], ['10.136.161.80', '2015-08-29'], ['10.136.161.80', '2015-08-29'], ['10.136.161.80', '2015-08-29'],['10.136.161.235', '2016-03-12'], ['10.136.161.235', '2015-05-02'], ['10.136.161.93', '2016-03-12'], ['10.136.161.93', '2016-03-12'], ['10.136.161.93', '2015-04-25'], ['10.136.161.93', '2015-04-25'], ['10.136.161.93', '2016-03-12'], ['10.136.161.93', '2015-04-25'], ['10.136.161.93', '2015-04-25'], ['10.136.161.93', '2015-11-28'], ['10.136.161.93', '2015-11-28'], ['10.136.161.80', '2015-08-29'], ['10.136.161.112', '2015-04-25'], ['10.136.161.231', '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/cleandata.py", line 18, in <module>
    fl = set(array)
TypeError: unhashable type: 'list'

Thanks in advance.

Amjad Syed
  • 185
  • 1
  • 1
  • 8

1 Answers1

3

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Thanks Rohit this solved my problem. now my data looks like this here we have different date and ip is same is it possible to keep only one date and IP 2015-08-29 10.136.161.80 2015-04-25 10.136.161.93 2015-04-25 10.136.161.231 2015-11-28 10.136.161.93 2016-04-02 10.136.161.231 2015-08-08 10.136.161.231 2015-11-28 10.136.161.235 2016-03-12 10.136.161.235 2015-04-25 10.136.161.112 2015-05-02 10.136.161.235 2016-03-12 10.136.161.93 2015-11-28 10.136.161.231 2016-03-12 10.136.161.231 – Amjad Syed Apr 24 '16 at 06:29