-2

How do I find unique items for a list of lists?

In the following example I expect only 2 items.

mylist=[[' Dish Towel .\n', '1.000', '149.000'],
 [' Dish Towel .\n', '1.000', '149.000'],
 [' Kitchentowel(mix designs) .\n', '1.000', '99.000'],
 [' Kitchentowel(mix designs) .\n', '1.000', '99.000']]

Expected result:

newlist=[[' Dish Towel .\n', '1.000', '149.000'],
 [' Kitchentowel(mix designs) .\n', '1.000', '99.000']]

I tried this but I got TypeError.

  output = set()
  for x in mylist:
       output.add(x)
  print output
shantanuo
  • 31,689
  • 78
  • 245
  • 403

2 Answers2

5

You can use set to preserve the unique items :

>>> set(map(tuple,mylist))
set([(' Kitchentowel(mix designs) .\n', '1.000', '99.000'), (' Dish Towel .\n', '1.000', '149.000')]) 

Note that since set just accept hashable objects you need to covert the lists to tuple then use set

Mazdak
  • 105,000
  • 18
  • 159
  • 188
3

You could try something like:

output = []
for x in mylist:
    if x not in output:
        output.append(x)
print output
NendoTaka
  • 1,224
  • 8
  • 14
  • This works, but runs in worst-case O(n^2) time, so it may not be preferable for especially large lists. – Kevin Oct 16 '15 at 13:18
  • worst-case should only be O(n) because there is only one loop. – NendoTaka Oct 16 '15 at 13:19
  • 2
    O(n) for the loop, and O(n) for the `in` operation, combines to form O(n^2) – Kevin Oct 16 '15 at 13:20
  • Your right I forgot about that. I thought that this may be useful because it would keep the output in the same format as the input instead of a set of tuples but your are right it may be a little slow. – NendoTaka Oct 16 '15 at 13:22
  • Yes, keeping the original typing is possibly an advantage. As well as maintaining the original ordering of the items, which may or may not be important to the OP. – Kevin Oct 16 '15 at 13:24