Python - Vérifier si la clé existe dans le dictionnaire
En Python, vous pouvez utiliser l'opérateurin pour vérifier si une clé existe dans un dictionnaire.
test.py
def main():
fruits = {
'apple':1,
'orange':2,
'banana':3
}
#if key 'apple' exists in fruits?
if 'apple' in fruits:
print(fruits['apple'])
if __name__ == '__main__':
main()
Sortie
1
P.S Tested with Python 3.4.3
Notehas_key() est déconseillé au profit de la cléin d.