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

need a quick help

how to solve this?

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(string):
    return len(string)
def num_courses(strin):
    return len(strin.items())

3 Answers

Robert Daly
Robert Daly
2,287 Points

def num_teachers(str1): return len(str1)

def num_courses(str1): count = 0 for teach in str1: count = count + len(str1[teach])
return count

Hie Aishwari

Your argument is a dictionary, your first function is just returning the length of that dictionary instead. Try using the .keys() method to get a list of the dictionary's keys which list will be a list of teachers. return the length of that list as shown below.

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

For your second task you need to iterate over the dictionary's values, ie for a dictionary, dicto we do dicto.values(). Remember to have a variable that will increament as you iterate over that value, try:

def  num_courses(dicto): 
    count = 0
    for val in dicto.values(): # iterate over the dictionaries values one at a time
        count += len(val) # increament the variable count by the length of the value

    return count