Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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