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

Asher Orr
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,086 Points

Iterating over only the keys in a dictionary - is there an alternate way to do this, or is there an error w/ this quiz?

Hi everyone! On this quiz, the question asks:

Complete the code so it will iterate over only the keys in the student dictionary.

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

for key in student.#in between these hashtags, there is a space where you enter your answer#():

I thought the way to complete this code is:

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

for key in student:

I ran this in my workspace, and it returned only the keys. I think the quiz may have an error, because it currently forces user to enter text after "student." in the for loop.

To complete the challenge, you could just replace the period after "student" with a colon.

Is this correct? Or am I missing something about iterating over only the keys in the student dictionary?

1 Answer

Steven Parker
Steven Parker
218,861 Points

Just naming the dictionary would also do the same job, but since there's a membership operator before the blank, they are expecting you to use the "keys()" method there.