"Introduction to jQuery" was retired on January 31, 2018. You are now viewing the recommended replacement.

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

Denis Frunz
Denis Frunz
15,929 Points

How can I count items in list inside of dictonary?

I have no idea how I can do this, work all day long and still nothing....

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 (dict_teacher):
    count = 0
    for key in dict_teacher.keys():
        count += 1    
    return count
def num_courses (dict_teacher):
    count = 0
    for key in dict_teacher.values():
        count += 1    
    return count

3 Answers

Steven Parker
Steven Parker
231,269 Points

It looks like you're trying to use almost the exact same code that counts the teachers to count the courses.

But to count the courses, you'll need to get a count of courses for each teacher and then add those all up together.

Denis Frunz
Denis Frunz
15,929 Points

first function counts how many teachers we have, second function mustt count how many courses we have in total, so my functions counts in total 3 ccourses that'snot right I need to understand how I can count items in these lists

Steven Parker
Steven Parker
231,269 Points

The code you have now counts the entire list of courses for each teacher as one thing. So one approach you might use is to have another loop inside that one count each course in the list.

Besides counting the courses one at a time, another choice might be to use the length of the list.

Denis Frunz
Denis Frunz
15,929 Points

this is what I did ,it works well in workspace but in a challenge I keep run into "Bummer: Try again!"

# 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(teacher_dict):
    count = 0
    for key in teacher_dict.keys():
        count +=1
    return count

def num_courses(teacher_dict):
    count = 0
    for value in teacher_dict.values():
        for i in value:
            count +=1
    return count
Steven Parker
Steven Parker
231,269 Points

That looks OK to me. So I pasted it directly into the challenge and it passed task 2!

Try again?

Denis Frunz
Denis Frunz
15,929 Points

When I reload a page it worked )