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

Ryan Ruscett
Ryan Ruscett
23,309 Points

What is in this dictionary

Hello,

I may be going crazy, but help me out here.

So, at first I thought the dictionary looked like this.

dict = {"Adam" : 7, John : 8, etc}

So I iterated through the dict and put the values into a list. Did a max on the list and found the highest number. Then I did a loop through the keys to return the value only if it was the max value. So needless to say this did not work. Didn't get the right teacher.

I then thought, ohh so it must be value then teachers. So like this

dict = {7 : "Adam", 8 : "John}

Well, again, I found the highest key and then got the value. This then returned a whole bunch of classes when it expected a name.

So now I am confused. It appears to me that it's asking me to count the number of classes each teacher has and print the teacher with the most classes.?

Now unfortunately the preview doesn't work. If I try to print the entire list just to see what I am working with. It just shows a black screen. At least on chrome. So I can't see the list I am trying to get. This makes it increasingly difficult to figure out.

Anyone have any suggestions.

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(dictOfTeachers):
  T = []
  for thing in dictOfTeachers:
    T.append(thing)

  A = max(T)
  I = dictOfTeachers.get(A)


  return I;

2 Answers

Dan Johnson
Dan Johnson
40,533 Points

The dictionary being passed in will be similar to the first example in the comment section. The teacher's name is the key, and the value is a list of courses. So here are some ideas:

def most_classes(teachers):
  # Keep track of the leading teacher
  # Keep track of the leading teacher's course count

  for key, value in teachers.items():
    # If the length of the courses is longer
        # Update the leading teacher and course count

  return top_teacher
Ryan Ruscett
Ryan Ruscett
23,309 Points

Thanks man,

Yeah I totally didn't even read that lol. I am going crazy. I knew it! I just tossed it all into a list and used some index() and subtraction etc. Although a good hint would be try extends instead of append in some instances. Just saying for whoever might read this.