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

Now, create a function named Stats that takes the teacher dictionary.Return a list of lists in the format.

functions, dictionaries, collections

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(my_dict):
  teachers_list = {}
  for teacher in my_dict:
    teachers_list[teacher] = len(my_dict[teacher])
  return max(teachers_list, key = teachers_list.get)

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

def stats(teachers):
  num_classes = []
  for name, value in teachers.items():
    new_list = [name, len(value)]
    num_classes.append(new_list)
    return num_classes

2 Answers

rdaniels
rdaniels
27,258 Points

I worked the first three parts of the challenge, and passed them with the code below. If you need help on the fourth part, just let me know... Keep Coding!!!

def most_classes(teacherdict):
  newdict = {}
  for key, value in teacherdict.items():
      newdict[ len(value) ] = key
  return newdict[ max(newdict.keys()) ]

def num_teachers(teacherdict):
  return len(teacherdict)

def stats(teachers):
  num_classes = []
  for name, value in teachers.items():
    new_list = [name, len(value)]
    num_classes.append(new_list)
  return num_classes
Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello David,

you are pretty close so I won't tell you too much:

have a look the indentation of your return statement! With this indentation the loop stops after the first teach only.

;)

Vittorio

Thanks Vittoria i got that part sorted but i get a bummer still "Bummer! 'dict' object has no attribute 'key'

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

You should NOT touch anything else, just the indentation of the return statement.