Artificial intelligent assistant

Pythonでdictから存在しない可能性があるキーの削除 PythondictdelKeyError >>> a = dict(a=1, b=2) >>> del a["c"] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'c' >>> a = dict(a=1, b=2) >>> if "c" in a: ... del a["c"] ... >>>

`dict.pop`


a = dict(a=1, b=2)
a.pop('c', None) # None
print a # {'a': 1, 'b': 2}
a.pop('a', None) # 1
print a # {'b': 2}


> If `key` is in the dictionary, remove it and return its value, else return `default`. If `default` is not given and key is not in the dictionary, a KeyError is raised.

`pop(key[, default])` `default` `pop` 2 `default` `KeyError` `None`

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy ff571ba572d5677fc1dab602dde09fc3