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

后端 未结 5 803
栀梦
栀梦 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:01

    The difference is that if the key is missing, d[key] will raise a KeyError exception, whereas d.get(key) will return None (and d.get(key, default) will return a default value).

    There are no noticeable differences in memory requirements.

提交回复
热议问题