1. Learn
  2. /
  3. Courses
  4. /
  5. Data Types for Data Science in Python

Exercise

Working with dictionaries more pythonically

So far, you've worked a lot with the keys of a dictionary to access data, but in Python, the preferred manner for iterating over items in a dictionary is with the .items() method.

This returns each key and value from the dictionary as a tuple, which you can unpack in a for loop. You'll now get practice doing this.

We've loaded a squirrels_by_park dictionary, and the Madison Square Park key contains a list of dictionaries.

Instructions

100 XP
  • Iterate over the first record in squirrels_by_park["Madison Square Park"], unpacking its items into field and value.
    • Print each field and value.
  • Repeat the process for the second record in squirrels_by_park["Union Square Park"].