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!

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

Michael Escoto
Michael Escoto
30,028 Points

Teacher Stats Task 1 of 3

I'm completely stumped on how to make this work.

Am I on the right path or can I refactor this code to make this easier?

def most_classes(dicts):
 teachers = list(dicts.keys())
 classes = list(dicts.values())
 max_count = 0
 i = 0
 j = 0
 for items in classes:
  if len(classes[i]) > max_count:
   max_count = len(classes[i])
 i += 1
 for teacher in teachers:
  if ____ == max_count:
   return teacher[j]
 j += 1 

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I don't want to give the whole answer away. :D Take a look at this:

counts = []
for key in my_dict:
    counts.append(len(my_dict[key]))

Now counts will hold onto the length of the value of each key in the dictionary. You'll need some functionality similar to this to solve the code challenge.

Michael Escoto
Michael Escoto
30,028 Points

It took a while for it to sink in but I finally got it. Thank you.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Michael Escoto Awesome!

I know it can be a little frustrating sometimes but great job sticking with it. If there was any part of it that you felt was way too confusing/hard, let me know. I'm more than happy to improve the course as much as I can.

I found the dictionary confusing since it's eccentrically modeled?:

Shouldn't it be designed the following way to be more usable, more believable (that it came from a database and that they're related to each other, and more easily programmable towards being more applicable beyond the challenge?

 {name: "Teacher Name", classes: ["class1", "class2", "class3", "class4" } 

I'm still confused by this challenge, but that's great: I never had a problem with such a max count challenge before (It's humbling even).

To clarify I was able to get the following to work:

def most_classes(teachers):
  counts = []
  max_classes = 0 
  for key in teachers:
    counts.append(len(teachers[key]))

  for value in counts:
    if value > max_classes:
      max_classes = value 

  for key in teachers:
    if max_classes == len(teachers[key]):
      return key

The problem with this is python3 gives me the following error outside of the challenge (I prefer completing the challenges with Vim):

TypeError: tuple indices must be integers, not dict. 

This may need to be considered with future challenges involving iterating through a dictionary; I again think an array of dictionaries with name and classes more intuitive and more pragmatic than the current dictionary for this problem.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Kevin Lozandier if it was modeled that way, we'd need one dict per teacher. As it is, we only need one dict for the whole teaching staff.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Let's break it down outside of programming.

You have a directory of teachers with all of their classes listed after them. Or maybe it's actors and their awards. Or car manufacturers and their models of cars. As you read through the list, you count the number of things they have and keep track of who has the most and how many they had.

So do that here. Hold onto the biggest number of classes (this would start at 0) and what teacher has those (this would start as None or just an empty string). Now go through each teacher in the dictionary and see if they have a larger roster of classes than your current highest count. If so, up the high count and change the saved teacher name. If not, move on to the next teacher.

Michael Escoto
Michael Escoto
30,028 Points

Okay, conceptually I get the idea but I'm not sure how to get there programmatically.

One problem I keep running into is counting the number of values inside the list inside the dictionary. If I run the program one time it will come back with a count of 2 the first time and 3 the next time.