Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Dictionaries Teacher Stats

Why does this only work some times?

This will past every third time or so. What am I doing wrong?

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(teachers):
  max_count = 0
  busiest_teacher = ''
  for teacher in teachers:
    if len(teachers.values()) > max_count:
      max_count = len(teachers.values())
      busiest_teacher = teacher
  return busiest_teacher

3 Answers

Ryan Merritt
Ryan Merritt
5,789 Points
teachers.values()

will return a list of all values in the dictionary, not the value of a particular key. Hint: use the teacher variable to access the value of that particular teacher from the dict.

Thank you. It worked. Why does it work some of the time? Dumb luck depending on the list?

Ryan Merritt
Ryan Merritt
5,789 Points

Consider the following lines of code I inputted into the python interpreter:

>>> teachers = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections']}
>>> teachers.values()
dict_values([['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], ['Python Basics', 'Python Collections']])

When I asked python for teachers.values() it returns every value in the dictionary, which in this case is comprised of two lists.

Now consider:

>>> for teacher in teachers:
...   print(len(teachers.values()))
... 
2
2
>>> for teacher in teachers:
...   print(len(teachers[teacher]))
... 
3
2

The first loop loops through the items of the dict (it loops twice because there are two items), then prints out the number of total values TWICE. Note that this is the length of every value in the dict, NOT the length of the list inside a dict item.

The second loop also loops twice (for each item in the dict), but instead prints out the length of teachers[teacher].

I have yet to have my evening cup of coffee so forgive the potentially confusing response :D

Great! Thanks! This really helped for the 4th part too :-)