How can I add new keys to a dictionary?

前端 未结 16 2682
梦毁少年i
梦毁少年i 2020-11-22 00:40

Is it possible to add a key to a Python dictionary after it has been created?

It doesn\'t seem to have an .add() method.

16条回答
  •  旧时难觅i
    2020-11-22 01:10

    first to check whether the key already exists

    a={1:2,3:4}
    a.get(1)
    2
    a.get(5)
    None
    

    then you can add the new key and value

提交回复
热议问题