Sous python pour vérifier si une clé existe ou non dans un dictionnaire il suffit d'utiliser l'opérateur in, exemple:
>>> d = {'abstract':'hello world !', 'link':'/welcome/','priority':2}
>>> 'abstract' in d
True
>>> 'content' in d
False
>>> 3 in d
False
>>> 'priority' in d
True
l'utilisation de l'opérateur in donne un boolean que l'on peut alors utiliser avec if par exemple:
if 'priority' in d:
# do something
else:
# do something else !
Références
Liens | Site |
---|---|
operator — Standard operators as functions | doc python |
Check if a given key already exists in a dictionary | stackoverflow |
Most efficient method to check if dictionary key exists and process its value if it does | stackoverflow |
Check key exist in python dict | stackoverflow |
Use and meaning of “in” in an if statement? | stackoverflow |