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

Rabih Atallah
Rabih Atallah
3,405 Points

please any help is appreciated! on how to start this code challenge and how to return the key of the longest list

Thank you

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.

2 Answers

Hi Rabih,

You will need to create a function that takes teacher_dict as an argument and does the following:

---- assigns initial values to two variables: max_count = 0 and max_teacher = ' '

---- uses a for loop to look at each key-value pair to determine if the length of the value is greater than max_count. If it is, then max_count is updated with the length of the value (the number of courses) and max_teacher is assigned the key (the teacher's name) associated with that value.

sample code snippet for the for loop:

 for key, value in teacher_dict.items():
    if len(value) > max_count:
        max_count = len(value)
        max_teacher = key

---- the function should return the value max_teacher

Rabih Atallah
Rabih Atallah
3,405 Points

Thank you Tree for your answer!! It was helpful. I ended up writing this code which worked: def most_classes(teachers): max_count = 0 G = [] for k in teachers.keys(): max_count = len(teachers[k]) G.append(max_count)

for i in teachers.keys():
    if len(teachers[i]) == max(G):
        return i

Glad you were able to get it to work! :-)