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

How do i complete the code so it iterates over only the values in the student dictionary.

I want to fill in the missing word of the following code:

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

for val in student.____(): print(val)

2 Answers

Steven Parker
Steven Parker
207,989 Points

Believe it or not, the name of the dictionary method that returns just the values is ... "values".   :wink:

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 26,279 Points

Often, you will want to know the key and value at the same time, so it's convenient to use a programming pattern that can serve double duty (e.g. the student.items() iterator).

student={'name': 'Craig', 'major': 'Computer Science', 'credits': 36}
# print just values
for value in student.values():
   print(value)

# print just the keys
for key in student.keys():
    print(key)

# print key and value
for key, value in student.items():
    print(key, value)