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

Pau Diaz Gallifa
Pau Diaz Gallifa
1,616 Points

Why the courses should be 18 and not 5? in my_dict I only have 5!! There is anything I did not understand?

Why the number of courses should be 18? where is the dict with all these teachers?

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.
my_dict = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
'Kenneth Love': ['Python Basics', 'Python Collections']}

busy_teacher=''  
def most_classes(my_dict):
  max_count = 0
  for x,y in my_dict.items():
      if len(y) > max_count:
        max_count = len(y)
        busy_teacher = x  
  return busy_teacher 


def num_teachers(my_dict):
  for thing in my_dict:
    number_teachers = len(my_dict)
  return number_teachers

def stats(my_dict):
  listoflists = []  
  for teachers in my_dict:
      listoflists.append([teachers, len(my_dict[teachers])])
  return listoflists


def courses(my_dict):
  courses = []
  for values in my_dict.values():
      courses.append(values)
  return courses

3 Answers

Hi Pau Diaz,

The challenge is going to use its own dictionary and not the one that you have created. So you don't need to create that dictionary and you should move the busy_teacher initialization inside the most_classes function so that it is self contained and not relying on a global variable.

The problem with your courses function is that you're creating a list of lists rather than a list of courses. Each time through the loop you're appending the list values to your courses list.

Instead you want to extend the list courses.extend(values)

Alternatively, you could add the values list to the courses list each time courses += values

Adding 2 lists together creates a new list with the values from each one.

Pau Diaz Gallifa
Pau Diaz Gallifa
1,616 Points

Thank you Jason Anello , the problem was that, challenge is using its own dictionary instead of mine. Thanks!!!

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Looking at your last function:

def courses(my_dict):
    courses = []
    for values in my_dict.values():
        courses.append(values)
    return courses

The values used in the for-loop is the value portion for each (teacher) key in my_dict. This value is a list. Using append() will add an item in whole as new member of the list courses. This means your are creating a list of lists. When combining the items in one list with another, use extend():

        courses.extend(values)

So is the idea here that the challenge utilizes its own dictionary that somehow shares the same name as the dictionary you create? I still don't think I understand why and how this challenge procures a dictionary with 18 courses in it.

I was able to pass the challenge with the advice already provided here, but I don't understand how 18 courses came out of nowhere.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

The grader exercises your code by calling it with test data, like:

most_classes(testdict)

The dict you mention "creating" is actually a placeholder parameter of your function that get assigned the value presented by the statement calling the function.

The variable name holding the value of the called value is unrelated to the name chosen for the function parameter. They are in different variable namespaces.

thanks chris, that makes so much more sense now!