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

Create a function named most_classes that takes a dictionary of teachers. Each key is a teacher's name and their value i

Create a function named most_classes that takes a dictionary of teachers. Each key is a teacher's name and their value is a list of classes they've taught. most_classes should return the teacher with the most classes.

I am stuck for more than a day now! Please help! Here is my code:

td = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections']}

def most_classes(td):

tempv = []
k = list(td.keys())  # <-- turning all keys in the dictionary into a new list -  k = []
v = list(td.values())# <-- turning all values in the dictionary into a new list - v = [[]], output is list within a list
i = len(v) # <-- defining the length of a sub_list

for sub_list in v: # <-- looping though sub_lists
    tempv.append(len(sub_list[0][i])) # <-- replacing names of subjects (values) with an int (just a number of subjects), 
                                      # <-- and turning it into a new list tempv = [] where numbers reflect number of subjects taught by each teachers
return k[tempv.index(max(tempv))] # <-- returning the corresponding index value in the teachers names (keys in dictionary)

3 Answers

Krzysztof Kucharzyk
seal-mask
.a{fill-rule:evenodd;}techdegree
Krzysztof Kucharzyk
Front End Web Development Techdegree Student 4,005 Points

This should work here :

def num_teachers(my_dict):
    return(len(my_dict))

def most_classes(my_dict):
    max_count  = 0
    for key in my_dict:
        if len(my_dict[key]) > max_count:
            max_count = len(my_dict[key])
            answer = key
    return answer
Jeffery Briggette
PLUS
Jeffery Briggette
Courses Plus Student 5,567 Points

Great answer, this is an easier snippet of code to understand :

    def most_classes(teacher_dict):
      max_count = 0
      best_teacher = ''
      for teacher in teacher_dict:
        if len(teacher_dict[teacher]) > max_count:
               best_teacher = teacher
      return best_teacher

dict1 = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections']} def most_classes(dict1): dict1_keys = dict1.keys() dict1_values = dict1.values() dict1_list_name = [] dict1_list_value = [] dict1_name_value = [] for i in dict1_keys: print(i,':') dict1_list_name.append(i) for j in dict1[i]: print(j) dict1_list_value.append(j) a_1 =str(dict1_list_name[0])+':'+str(dict1_list_name[0]) a_2 = str(dict1_list_name[1])+':'+str(dict1_list_name[1]) return a_1+a_2 most_classes(dict1) I right this, I test in my python3.4, It works well, but the website system said I make issue. I have no idea the reason when I test on compute work well and give me correct answer but not work well on the website.

David Leal
David Leal
1,990 Points

sorry mane but you have to update the value on max_count or it will be always 0

cheers

rd. ln.
rd. ln.
7,851 Points

def most_classes(teacher_dict): max_count=0 max_teacher='' for teacher in teacher_dict: if len(teacher_dict[teacher])>max_count: max_count=len(teacher_dict[teacher]) max_teacher=teacher return max_teacher