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 (Retired) Dictionaries Teacher Stats

Justin Noor
Justin Noor
3,692 Points

My 'stats' function only generates one list.

I've been breaking my head on this one for a few. Can anyone give me some hints as to why my last function does not pass? This is a three part challenge, and my first two functions pass. The third function is supposed to generate a list of lists from the dictionary (mydict).

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.

def most_classes(mydict):
    num_classes = 0
    busy_teacher = ''
    for k, v in mydict.items():
        if len(v) > num_classes:
            num_classes = len(v)
            busy_teacher = k
    return busy_teacher

def num_teachers(mydict):
    return len(mydict.keys())

def stats(mydict):
    for k, v in mydict.items():
        newlist = []
        newlist.append([k, len(v)])    
        return newlist

3 Answers

Steven Parker
Steven Parker
231,269 Points

You've got all the right stuff there, but you need to fix the order and the indenting:

  • You want to declare "newlist" before the loop (fix the line order and indentation)
  • You want to return the new list after the loop (check the indentation)

I'll bet you can get it now without an explicit spoiler.

:point_right: And it's a four part challenge, there's one more task after this one.

Agreed with Steven Parker : Here are pieces of code that might help you.

def stats(mydict):
    newlist = list()
    for k, v in mydict.items():
        newlist.append([k, len(v)])    
    return newlist
Steven Parker
Steven Parker
231,269 Points

The original "newlist = []" is just fine when the placement is corrected.

Justin Noor
Justin Noor
3,692 Points

Awesome thanks guys. It passes now.

Now let's see if I can pass the fourth part lol.