Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Introducing Dictionaries Iterating and Packing with Dictionaries Recap of Iterating and Packing with Dictionaries

Jorge Grajeda
Jorge Grajeda
1,226 Points

How would I answer this problem?

May somebody please explain a little bit more in depth on these keys and values for this problem?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,464 Points

If given:

some_dict = {
    key1: value1,
    key2: value2,
    key3: value3,
    }

There are three ways to iterate on a somedict:

  • iterate over the keys using dict.keys()
# default method. same as using
#     for key in some_dict:
for key in some_dict.keys():
    print(key)

# output
key1
key2
key3
  • iterate over the values using dict.values()
for value in some_dict.values():
    print(value)

# output
value1
value2
value3
  • iterate over both the keys and values using dict.items()
for key, value in some_dict.items():
    print(key, value)

# output
key1 value1
key2 value2
key3 value3

Post back if you need more help. Good luck!!!