Python Dictionary DataStructure which method d[] or d.get()?

后端 未结 5 768
栀梦
栀梦 2020-12-16 11:43

While Using Python Dictionary DataStructure (which contains key-value pair) if i want to retrieve some value from my Dictionary i have two options d[\'\'] and g.get(\'key

5条回答
  •  失恋的感觉
    2020-12-16 12:05

    They both behave the same if the key exists. But if the key is not found d['key'] will raise a KeyError exception, whereas d.get('key') will return None. You can also supply a second argument to the get method which will be returned on a not-found condition: d.get('key', '') will return a null string if the key is not found.

提交回复
热议问题