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

Jorge Grajeda
Jorge Grajeda
1,068 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 64,074 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!!!