Python - Flatten a dict of lists into unique values?

后端 未结 7 2236

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条回答
  •  旧时难觅i
    2020-12-30 23:14

    use set() and itertools.chain():

    In [83]: content = {88962: [80, 130], 87484: [64], 53662: [58,80]}
    
    In [84]: from itertools import chain
    
    In [94]: x=set(chain(*content.values()))
    
    In [95]: x
    Out[95]: set([58, 64, 80, 130]) # a set, the items may or may not be sorted
    
    In [96]: sorted(x)         #convert set to a sorted list
    Out[96]: [58, 64, 80, 130]
    

提交回复
热议问题