Session Ready
Exercise

Checking dictionaries for data

You can check to see if a key exists in a dictionary by using the in expression.

For example, you can check to see if 'cookies' is a key in the dictionary by using if 'cookies' in recipes_dict: this allows you to safely react to data being present in the dictionary.

You can also use the in expression so see if data is in the value of a dictionary such as if 'cookies' in recipes_dict.values(). Remember you have to handle nested dictionaries differently as illustrated in the video and previous exercises, and use the in expression on each nested dictionary.

Instructions
100 XP
  • Check to see if 2011 is in the baby_names dictionary.
    • Print 'Found 2011' if it is present.
  • Check to see if 1 is in baby_names[2012].
    • Print 'Found Rank 1 in 2012' if found and 'Rank 1 missing from 2012' if not found.
  • Check to see if rank 5 is in baby_names[2013].
    • Print 'Found Rank 5' if it is present.