Python - Dictionary - Does key exist?


/ Published in: Python
Save to your folder(s)

Snippet to verify whether or not a key exists in a python dictionary.


Copy this code and paste it in your HTML
  1. myFavoriteGhibliMoviesReleaseYearDict = {
  2. 'Whisper of the Heart' : '1995',
  3. 'Princess Mononoke' : '1997',
  4. 'Spirited Away' : '2001'
  5. }
  6.  
  7. if 'Spirited Away' in myFavoriteGhibliMoviesReleaseYearDict:
  8. print('\'Spirited Away\' release year: ' + myFavoriteGhibliMoviesReleaseYearDict['Spirited Away'])
  9. else:
  10. print('\'Spirited Away\' release year not found.')

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.