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

How do I return key based on a value?

My most_courses function isn't passing, but it works in my own module.

What's up with that?

Thanks.

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(treeDict):
    return len(treeDict.keys())

def num_courses(treeDict):
    count = 0
    for v in treeDict.values():
        count += len(v)
    return count

def courses(treeDict):
    courseList = []
    for v in treeDict.values():
        for course in v:
            courseList.append(course)
    return((courseList))

def most_courses(treeDict):
    theBigOne = max(len(value) for value in treeDict.values())
    teachers = []
    for key, value in treeDict.items():
        if len(value) == theBigOne:
            teachers.append(key)
    return teachers

1 Answer

andren
andren
28,558 Points

The challenge expects the method to return a single name, not a list of names. So it is expecting a string in return, not a list. If you convert your list to a string like this:

def most_courses(treeDict):
    theBigOne = max(len(value) for value in treeDict.values())
    teachers = []
    for key, value in treeDict.items():
        if len(value) == theBigOne:
            teachers.append(key)
    return ''.join(teachers)

Or not make it a list in the first place like this:

def most_courses(treeDict):
    theBigOne = max(len(value) for value in treeDict.values())
    teacher = ''
    for key, value in treeDict.items():
        if len(value) == theBigOne:
            teacher = key
    return teacher

Then your code will be accepted.