Python - Flatten a dict of lists into unique values?

Posted by Jonathan Vanasco on Stack Overflow See other posts from Stack Overflow or by Jonathan Vanasco
Published on 2012-10-22T16:58:50Z Indexed on 2012/10/22 17:00 UTC
Read the original article Hit count: 218

Filed under:

I have a dict of lists in python:

content = {88962: [80, 130], 87484: [64], 53662: [58,80]}

I want to turn it into a list of the unique values

[58,64,80,130]

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 ?

content = {88962: [80, 130], 87484: [64], 53662: [58,80]}
result = set({})
for k in content.keys() :
    for i in content[k]:
        result.add(i)
# and list/sort/print just to compare the output
r2 = list( result )
r2.sort()
print r2

© Stack Overflow or respective owner

Related posts about python