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

so far my function can count the total number of classes,....how can i count a single value?

better explanation is i understand that my function only works with list. i understand that if the key's value is a string it will iterate through that as well.

my problem is overcoming it im wrecking my brain made several functions but all in essence do the same thing count number of items in list but if i create a key with a single string value then it counts the letters spaces...how can i count the keys value alone and list of values?

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(a_dict):
    return len(a_dict.keys())

def num_courses(a_dict):
    new = []
    for key,value in a_dict.items():
        for values in a_dict[key]:
            new.append(values)
    return len(new)    

1 Answer

Steven Parker
Steven Parker
231,269 Points

While iterating through the items, inside the loop you'll have both the "key" and the "value" list.

So while in the loop, "len(value)", would be the number of entries (classes) in that single value.

Right i understand that, but the problem im facing is if "len('Python Basics')" would give me back 13, but len(['Python basics','python collection']) returns 2

im assuming i cant pass the challenge because if a key with a single value is used it wont return the right number? i hope im not confusing or frustrating you im really stuck on this one

Steven Parker
Steven Parker
231,269 Points

There's a difference between the length of a string and the length of a list. While "len('Python Basics')" would be 13, "len(['Python Basics'])" would just be 1.

No matter how many entries it has, the value associated with each key in the dictionary will be a list.