I have a dict of lists in python:
JavaScript
x
2
1
content = {88962: [80, 130], 87484: [64], 53662: [58,80]}
2
I want to turn it into a list of the unique values
JavaScript
1
2
1
[58,64,80,130]
2
I wrote a manual solution, but it’s a manual solution. I know there are more concise and more elegant way to do this with list comprehensions, map/reduce , itertools , etc. anyone have a clue ?
JavaScript
1
10
10
1
content = {88962: [80, 130], 87484: [64], 53662: [58,80]}
2
result = set({})
3
for k in content.keys() :
4
for i in content[k]:
5
result.add(i)
6
# and list/sort/print just to compare the output
7
r2 = list( result )
8
r2.sort()
9
print r2
10
Advertisement
Answer
Double set comprehension:
Python 3:
JavaScript
1
2
1
sorted({x for v in content.values() for x in v})
2
Python 2:
JavaScript
1
2
1
sorted({x for v in content.itervalues() for x in v})
2