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 dTrue>>> 'content' in dFalse>>> 3 in dFalse>>> 'priority' in dTrue
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 somethingelse:# 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 |
