switch key and values in a dict of lists

前端 未结 3 483
孤街浪徒
孤街浪徒 2021-01-26 18:50

Hello Stackoverflow people,

I have a nested dictionary with lists as values and I want to create a dict where all the list entries get their corresponding key as value.<

3条回答
  •  爱一瞬间的悲伤
    2021-01-26 19:23

    Use dictionary comprehension:

    d = {'a': 'b', 'c': 'd', 'e': 'f'}
    d2 = dict((v1, k) for k, v in d.items() for v1 in v)  # Here is the one-liner
    

提交回复
热议问题