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

Kimmo Ojala
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

Keys and values in dictionary

Don't really know why this doesn't work.

BR, Kimmo

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(dictionary):
    return len(dictionary)

def num_courses(dictionary):
    courses = []
    for value in dictionary.values():
        courses = courses + value
    return len(courses)    

def courses(dictionary):
    courses = []
    for value in dictionary.values():
        courses = courses + value
    return courses

def most_courses(dictionary):  
    teacher_w_most_subjects = None
    highest_number_of_subjects = 0
    for key in dictionary.keys():
        value = dictionary[key]
        number_of_subjects = len(value) 
        if number_of_subjects > highest_number_of_subjects: 
            teacher_w_most_subjects = key 
    return teacher_w_most_subjects    

def stats(dictionary):
    inner_list = []
    outer_list = []
    for key in dictionary.keys():
        inner_list.append(key)
        inner_list.append(len(dictionary[key])
        outer_list.append(inner_list)                  
    return outer_list          

4 Answers

Kimmo Ojala
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

Thanks it works now! Fixing the inner_list[] to be inside the loop makes sense, but I don't really understand why I do not (cannot) need to specify dictionary.keys() to get the value of the key as I did in the previous challenge (value = dictionary[key].

How come I get value of the key without specifying dictionary.keys()?

def most_courses(dictionary):
teacher_w_most_subjects = None highest_number_of_subjects = 0 for key in dictionary.keys(): value = dictionary[key] number_of_subjects = len(value) if number_of_subjects > highest_number_of_subjects: teacher_w_most_subjects = key return teacher_w_most_subjects

Thomas McDonnell
Thomas McDonnell
8,212 Points

Hi Kimmo, I don't think there is any need to specify dictionary.keys() if you are looping over them. Also should your inner_list not be inside the for loop itself? My own answer followed the same kind logic check out the amended below.

def stats(dictionary): outer_list = [] for key in dictionary: inner_list = [] inner_list.append(key) inner_list.append(len(dictionary[key])) outer_list.append(inner_list) return outer_list

Thomas McDonnell
Thomas McDonnell
8,212 Points

Hi Kimmo glad to hear its working for you know. I am still very much a beginner here too so don't take this as fact and do some digging yourself. From my understanding when we use a for loop in a dict we are looping over the keys and not the values. If we wanted to loop over the values we would us a nested loop i.e a for loop within a for loop

like this:

for key in dict: for v in key:

Kimmo Ojala
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

Hi Thomas,

Actually, I realized that there was a syntax error in my code, a missing parenthesis. This code works now:

def stats(dictionary): outer_list = [] for key in dictionary.keys(): inner_list = [] inner_list.append(key) inner_list.append(len(dictionary[key])) outer_list.append(inner_list)
return outer_list

Thanks for pointing out that its's also possible to loop over a dictionary and return values without using the dictionary.keys() method!

BR, Kimmo