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

Saul Goldman
Saul Goldman
1,539 Points

please help with teacher stats task :)

if you could attach a corrective code that would be amazing, also I know that what ive done assumes the dicts have an index which they do not but I hope you can see what ive tried to do and appreciate any help!

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

def num_courses(dicts):
    key_counter = -1
    total = 0
    for courses in dicts:
        keys = len(dicts[key_counter + 1])
        total = total + keys
        key_counter = key_counter + 1
    return total

1 Answer

Steven Parker
Steven Parker
231,275 Points

You're right dictionaries don't have a numeric index, but you can index them with one of their keys to get the associated value(s).

Also when you create a loop like "for courses in dicts:", that will iterate through the entire dictionary and "courses" will be set to the current key in each iteration.

Also remember that you're not counting the keys themselves (you did that in the last task), but the courses.

Hopefully, that'll get you back on track and you can take it from here.

Saul Goldman
Saul Goldman
1,539 Points

how do you index with keys as you suggested? and also I don't understand how I should alter the for loop?

Steven Parker
Steven Parker
231,275 Points

If "courses" is the key (that name might be a bit misleading since it's actually the teacher's name), then the list of courses would be "dicts[courses]". That's how you can use the key as an "index" to the dictionary item.

You can then change what the loop does to make it count the courses in each list and total them up.