Get unique values in List of Lists in python
44,777
Solution 1
array = [['a','b'], ['a', 'b','c'], ['a']]
result = {x for l in array for x in l}
Solution 2
You can use itertools
's chain
to flatten your array and then call set
on it:
from itertools import chain
array = [['a','b'], ['a', 'b','c'], ['a']]
print set(chain(*array))
If you are expecting a list
object:
print list(set(chain(*array)))
Solution 3
array = [['a','b'], ['a', 'b','c'], ['a']]
unique_values = list(reduce(lambda i, j: set(i) | set(j), array))
Solution 4
You can use numpy.unique:
import numpy
import operator
print numpy.unique(reduce(operator.add, [['a','b'], ['a', 'b','c'], ['a']]))
# ['a' 'b' 'c']
Solution 5
The 2 top voted answers did not work for me, I'm not sure why (but I have integer lists). In the end I'm doing this:
unique_values = [list(x) for x in set(tuple(x) for x in aList)]
Comments
-
mihasa over 3 years
I want to create a list (or set) of all unique values appearing in a list of lists in python. I have something like this:
aList=[['a','b'], ['a', 'b','c'], ['a']]
and i would like the following:
unique_values=['a','b','c']
I know that for a list of strings you can just use set(aList), but I can't figure how to solve this in a list of lists, since set(aList) gets me the error message
unhashable type: 'list'
How can i solve it?
-
mihasa almost 9 yearsthat did the job, thank you very much! Isn't there a better way to do this kind of think without needing to iterate? (i'm a beginner but i thought that avoiding loops was the way to go when having a big len(array))
-
mihasa almost 9 yearsi've just created my account, can't raise anything yet :( Your answer was very helpful, but ive accepted the first answer. when i have enough point (or idk what gives permition to raise answers) i'll do it for yours
-
Tanveer Alam almost 9 years@mihasa No worries. I'm glad my answer was helpful.
-
mihasa almost 9 yearsJust got it... raised!
-
dlask almost 9 yearsI am afraid there is no simpler way. You have a precisely defined input structure, you have a precisely defined output structure, and it's necessary to make the conversion. Since the conversion involves all elements it's necessary to "visit" all of them. Well, there are many different ways to obtain the result (see other posts) but the iteration is always present internally.
-
chepner almost 6 yearsThere's also
set(chain.from_iterable(array))
.