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

Serkan van Delden
Serkan van Delden
13,094 Points

teachers.py task 5

I have no idea how to do this, like no clue. I've been thinking about this for hours and an useful piece of code would be really appreciated since I've came this far. Thank you.

1 Answer

diogorferreira
diogorferreira
19,363 Points

This problem was quite confusing to me too when I was first starting out but I feel like it's just because there was so much information to take in and I was overcomplicating it.

  • To return a multidimensional list (list of lists) you first need to start out with one total = [] and then append or add onto the list.
  • Then you simply just loop through it to make find out the teacher's name and amount of coures
  • Teachers name is simply the key (which I split into the variable teacher) and course (into courses which you can then just find how many there are with len() )
  • Just add them onto the list you created at the start, but make sure you are appending a list, that's why there are extra [] around them otherwise your output would be - ['Andrew Chalkley', 2, 'Kenneth Love', 2]
  • And finalllyyyy return it!
def stats(dictionary):
    total = []
    for teacher, courses in dictionary.items():
        total.append([teacher, len(courses)])
    return total

#Equally you could do 

def stats(dictionary):
    total = []
    for teacher in dictionary:
        total.append([teacher, len(dictionary[teacher])])
    return total