For loop only addresses keys in a Python dictionary, not values

A quiz on Python dictionaries offered the following question:

What is the output of the following code?

oscars = {"Best Picture": "Moonlight", "Best Actor": "Casey Affleck", "Best Actress": "Emma Stone", "Animated Feature": "Zootopia"}
 
for element in oscars:
  print(element)

This piece of code returns printed keys. I’m not sure why the for loop takes into account only keys as an “element” here. This code does not use the .keys method. So why is not a “key-value” pair regarded as an element instead?

https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-dictionaries/quizzes/using-dicts

Iterating through a dictionary returns the keys by default. You can specify this with .keys(), get values instead with .values() or get key, value pairs as a tuple with .items(). See the docs for the details-

4 Likes

Thank you @tgrtim! :raised_hands: :pray: Initially I thought of a dictionary as a list of lists so to say, so assumed that key:value pairs are default items. Now I see I have missed that part in the documentation. Thanks again.

2 Likes