1

as a python list follow

list1 = [[1,2],[3,4],[1,2]]

I want make a set so I can the unique list items like

list2 = [[1,2],[3,4]].

Is there some function in python I can use. Thanks

Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
Yi Zhang
  • 219
  • 1
  • 2
  • 5
  • list() are mutable type and cannot be added to a set. You need to convert them to a tuple and then add them to a set(). – cedzz Aug 22 '16 at 14:17
  • What is the usecase? Will you update the lists? – awesoon Aug 22 '16 at 14:17
  • Funny that this question got more answers than the so called dupicate - in just one hour. What was the point to marking this as a duplicate? Just to keep people from proposing more ideas? – hpaulj Aug 22 '16 at 20:19

6 Answers6

3

That will do:

>>> list1 = [[1,2],[3,4],[1,2]]
>>> list2 = list(map(list, set(map(tuple,list1))))
>>> list2
[[1, 2], [3, 4]]
Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
1

Unfortunately, there is not a single built-in function that can handle this. Lists are "unhashable" (see this SO post). So you cannot have a set of list in Python.

But tuples are hashable:

l = [[1, 2], [3, 4], [1, 2]]
s = {tuple(x) for x in l}

print(s)

# out: {(1, 2), (3, 4)}

Of course, this won't help you if you want to later, say, append to these lists inside your main data structure, as they are now all tuples. If you absolutely must have the original list functionality, you can check out this code recipe for uniquification by Tim Peters.

Community
  • 1
  • 1
Pierce Darragh
  • 2,072
  • 2
  • 16
  • 29
1

Note that this only removes duplicate sublists, it does not take into account the sublist's individual elements. Ex: [[1,2,3], [1,2], [1]] -> [[1,2,3], [1,2], [1]]

>>> print map(list, {tuple(sublist) for sublist in list1})
[[1, 2], [3, 4]]
ospahiu
  • 3,465
  • 2
  • 13
  • 24
0

You can try this:

list1 = [[1,2],[3,4],[1,2]]
list2 = []
for i in list1:
    if i not in list2:
        list2.append(i)
print(list2)
[[1, 2], [3, 4]]
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48
  • This would perform poorly for large lists with many unique items, right? I'm not sure what the precise algorithmic complexity would be, but it approaches O(n^2) as the number of unique items approaches n, I think. – Pierce Darragh Aug 22 '16 at 14:22
  • @PierceDarragh You can always test performance. I checked it against your solution and my answer is faster. – Joe T. Boka Aug 23 '16 at 00:18
0

The most typical solutions have already been posted, so let's give a new one:

Python 2.x

list1 = [[1, 2], [3, 4], [1, 2]]
list2 = {str(v): v for v in list1}.values()

Python 3.x

list1 = [[1, 2], [3, 4], [1, 2]]
list2 = list({str(v): v for v in list1}.values())
BPL
  • 9,632
  • 9
  • 59
  • 117
  • Side note: `values()` will not give a list in Python 3, but a `dict_values` object – Moses Koledoye Aug 22 '16 at 14:32
  • @MosesKoledoye I know but OP didn't specify which python version he was using (python tag), so this is a perfectly valid answer ;-) . Anyway, let me edit – BPL Aug 22 '16 at 14:33
0

There is no inbuilt single function to achieve this. You have received many answers. In addition to those, you may also use a lambda function to achieve this:

list(map(list, set(map(lambda i: tuple(i), list1))))
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • As a warning: please don't use comments to ask people to vote for your answer. We just deleted over 50 of these, and they are nothing but noise. – Brad Larson Sep 01 '16 at 18:49