Python - Flatten a dict of lists into unique values?

后端 未结 7 2266

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 val

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-30 23:25

    sorted(set(val
                for row in content.itervalues()
                    for val in row))
    

    set gets us all the distinct values (like a dictionary, but without the overhead of storing values). sorted then just takes the created set and returns a list sorted in ascending order.

提交回复
热议问题