Is it possible to assign the same value to multiple keys in a dict object at once?

前端 未结 6 1901
时光取名叫无心
时光取名叫无心 2020-11-27 03:44

In Python, I need a dictionary object which looks like:

{\'a\': 10, \'b\': 20, \'c\': 10, \'d\': 10, \'e\': 20}

I\'ve been able to get th

6条回答
  •  猫巷女王i
    2020-11-27 04:21

    Similar to @SilentGhost but a more declarative syntax (with Python 3.5+) I prefer:

    myDict = {
      **dict.fromkeys(['a', 'b', 'c'], 10), 
      **dict.fromkeys(['b', 'e'], 20)
    }
    

提交回复
热议问题