switch key and values in a dict of lists

前端 未结 3 479
孤街浪徒
孤街浪徒 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:24

    assuming your key: value dictionary contains list as a value and using dict comprehension.

    Using a second loop to iterate over the list present in original dictionary.

    {item: key for key, value in dict1.items() for item in value}
    

提交回复
热议问题