Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

max lemelin
max lemelin
3,407 Points

Dictionaries, .values(), can't see why I'm wrong

.values() will give me all the values for the keys, which will be the number of course the teachers(keys) are teaching. Why is this answer wrong? thanks a lot for the help :)

teachers.py
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.


def num_teachers(teachers):
    return len(teachers.keys())

def num_courses(teachers):
    return len(teachers.values())

3 Answers

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Hello,

I think that the num_teachers function would be ok, is that what is failing?

num_courses will not be ok, as if you want the total number of courses, 4 in the example, you will get 2 as it is. If you are after the number of courses per teacher, then your return will be correct for the example given but the semantics of the code is incorrect.

max lemelin
max lemelin
3,407 Points

I still don't really get it unfortunately, i though len(values()) would give me 1 for each value which would equal 4.. but apparently its two. Thanks a lot for your answer.

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Hi Max,

len(teachers.values()) will return 2.

the teachers dictionary in the example has two keys, and two values, each of the values is a list. Each list contains two items.

to get the total number of courses, which is 4, you have to count the items in each of the lists

max lemelin
max lemelin
3,407 Points

Oh, thankyou very much. I wasn't aware the entire value section of a dictionary was treated like a list. one list, with two values in it. I'm just very confused a noob programmer. How's the tech degree working for you?

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Hi Max,

I don't think I explained this adequately - values of a dictionary are not treated as lists in general. In this case they are as it states in the task preamble that the value for each teacher key will be a list of courses.

Techdegree is OK, it got me actually developing larger projects as part of it, that was the main reason for doing it, but the content is available elsewhere on the site - would have been nice to have had a bit of techdegree only content.