Python - Flatten a dict of lists into unique values?

后端 未结 7 2231

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:18

    list(reduce(lambda a, b: a.union(set(b)), content.itervalues(), set()))
    

    The lambda turns the two input arguments into sets and unions them.

    The reduce will do a left fold over the list that is passed to it -- in this case, the lists that are the values of your dictionaries.

    The reduce will turn the result of this, which is a set back into a list.

    This can also be spelled:

    list(reduce(lambda a, b: a | set(b), content.itervalues(), set()))
    

提交回复
热议问题