Python dictionary creation syntax

前端 未结 7 1848
时光取名叫无心
时光取名叫无心 2020-12-30 18:25

I\'m wondering if there\'s any way to populate a dictionary such that you have multiple keys mapping to the same value that\'s less verbose than say:

d = {1:         


        
7条回答
  •  误落风尘
    2020-12-30 18:53

    Try to iterate through the dict items.

    • For python 2.x

    {key: value for key, value in your_dict.iteritems()}

    • For python 3.x

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

提交回复
热议问题