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 (2016, retired 2019) Dictionaries Teacher Stats

Get the max count of values and return the key in a dictionary

Hi, could you please help with my function most_courses() please. I am not sure why it does not pass

teachers.py
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.


def num_teachers(string):
    return len(string)

def num_courses(string):
    course_list=[]
    for x in string.values():
        for y in x:
            if y not in course_list:
                course_list.append(y)
    return len(course_list)

def courses(string):
    course_list=[]
    for x in string.values():
        for y in x:
            if y not in course_list:
                course_list.append(y)
    return course_list

def most_courses(string):
    maxcount = max(len(value) for value in string.values())
    return [k for k, value in string.items() if len(value) == maxcount]

2 Answers

Dear Zan,

checkout your format of the Answer it's a list. [Name] A quick fix will be

    return "".join([k for k, value in string.items() if len(value) == maxcount])

I hope this helps

Greetings Julian

Many thanks ! it works!