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

I am a bit confused

The question is kind of confusing because

student = {'name': 'Craig', 'major': 'Computer Science', 'credits': 36}

for key in student.():
    print(key)

I don't understand what I would put in the blank because if you ant just the key you would just do

for item in student:
     print(item)

and I don't see where we would use a method in this question but it is asking for a method. Thanks for any help you can offer. :)

1 Answer

Scott Bailey
Scott Bailey
13,176 Points

Because it's a dictionary the information is stored in 'key': 'value' pairs. So the key would be 'name' and the value 'Craig' (for example).

The question your on is asking for the key - so student.keys() is what their looking for.

It's true you can cycle through it like you mentioned - I found a good post on stackoverflow which might shed a bit more light on the matter here

In the quiz i think they just want you to be aware that you can use .keys() in this way. There may be more to it, maybe it's best practice to or something like that.