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!

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

John Weis
John Weis
3,929 Points

How to define parts of a dictionary & how to unpack lists inside of dictionaries?

After about two hours of banging away at keyboard & search engines, I'm stuck. No matter what I put in the "num_courses" either spits out the Teacher's name, the number of teachers, or just one set of lists instead of the four individual courses. How do I tell it to unpack the lists that are inside of a dictionary, how do I define("name"?) those lists, & how do I get my code to count those items inside of those lists? The code below is the simplest I've come up with. I wanted to spare the eye sore of the rabbit hole I fell down trying to solve this.

# 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.

teachers = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections']}

def num_teachers(dic):
    teacher = 0
    for key in dic:
        teacher += 1
    return teacher

def num_courses(dic):
    course = 0
    for value in dic:
        course += 1
    return course