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 trialSteven Olick
1,561 Pointsdoes not iterate through all values?
Hello. I've moved forward and solved this another way, but still don't quite get why this doesn't work to return the total number of values. I've also been doing this for around 4 hrs now so I could just be making a silly mistake. Thanks
# 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 her
def num_teachers(my_dictionary):
return int(len(my_dictionary.keys()))
def num_courses(my_dictionary):
return int(len(my_dictionary.values()))
2 Answers
Aaron Price
5,974 PointsI'm sure there's a more elegant way to do this, but it works.
def num_courses(my_dictionary):
count = 0
for teacher in my_dictionary:
count += len(my_dictionary[teacher])
return count
Jeremy Schaar
4,728 PointsHey Steven,
I'm just working through the challenge as well, so may not be the best to give advice, but looks to me like you're counting the number of values, not the number of items in each value. Python says "Andrew Chalkley" has one value ('jQuery Basics' and 'Node.js Basics'), and "Kenneth Love" has one value (Python Basics' and 'Python Collections'). So the length will be 2 (not 4).
I think Aaron's solution is the one they thought we were most likely to get to, but I'll just add in that the below also works. (I found it by googling and then had some fun reading up on the map function;) )
def num_courses(arg1):
return sum(map(len, arg1.values()))
Steven Olick
1,561 PointsThanks, Jeremy! I have seen the map() before but didn't read into it much. Won't hurt I'll need to learn it sometime!
Steven Olick
1,561 PointsSteven Olick
1,561 PointsThanks, Aaron. That was pretty much what I ended up writing to get through it. Always helps to see someone else had the same logic!