Get unique values in List of Lists in python

mihasa picture mihasa · Jun 1, 2015 · Viewed 33.2k times · Source

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?

Answer

dlask picture dlask · Jun 1, 2015
array = [['a','b'], ['a', 'b','c'], ['a']]
result = set(x for l in array for x in l)