Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introducing Dictionaries Iterating and Packing with Dictionaries Iterating Over Dictionaries

How do you write the a for loop that iterates over the provided dictionary.

The body of the loop should have two print statements: one to print the key and one to print the value of the current item

iterating_dictionaries.py
pets = {'name':'Ernie', 'animal':'dog', 'breed':'Pug', 'age':2}
for item in pets:
    print(item)
    print(pets.item())

2 Answers

Steven Parker
Steven Parker
229,018 Points

There are actually several ways to accomplish this objective, and you're getting close to one potential alternative. But even though the instructions don't mention it, this challenge specifically wants you to use the ".items()" method in your loop iterable.

The advantage to that approach is that it will return both the key and value at the same time, making both "print" statements very simple.