get() methode for retrieval (no exception):
Using get() methode there is no need to handle any exceptionreturn the value if exist, None if not.
ex:
dic
=
{
'key1'
:
'value1'
}
dic.get(
'key1'
)
# out: 'value1'
dict.get(
'key2'
)
# out: None
ex:
aValue = "i'm the replacement"
dic.get('key2',aValue)
# out: i'm the replacement
Test if a key exist:
We use if [] in []:key = "key2"
if key in dictionary:
print("key exist")
else:
print("key Don't exist")
Commentaires
Enregistrer un commentaire