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

Anthony Grodowski
Anthony Grodowski
4,902 Points

What's going on? Everything is working perfectly fine in the Workspace but the challange throws a bummer at me...

In the challange it says that my code returns wrong teacher name, but I've checked that in my Workspace with plenty of examples and it's working absolutely perfect. What's going on? Maybe if for example there are two teachers with most courses I should return both of them?

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

def num_courses(some_object):
    list_values = [v for v in some_object.values()]
    index = 0
    lol = 0
    while True:
        try:
            a = len(list_values[index])
            lol += a
            del list_values[index]
            continue
        except IndexError:
            break
    return lol

def courses(some_object):
    list_values_2 = [v for v in some_object.values()]
    pure_list = []
    index = 0
    while True:
        try: 
            pure_list.extend(list_values_2[index])
            index += 1
            continue
        except IndexError:
            break
    return pure_list

def most_courses(dic):
    maxcount = 0
    for teacher, courses in dic.items():
        if len(courses) > maxcount:
            maxcount = len(courses)

    for value in dic.values():
        if len(value) == maxcount:
            mc = value
    # Defining the teachers name

    for key, value_ in dic.items():
        if mc == value_:
            name = key
    teacher = name.split()
    teacher_name = teacher[0]
    return teacher_name

1 Answer

Steven Parker
Steven Parker
229,670 Points

The instructions say the function "should return the name of the teacher" (which would be a string), but this code seems to be returning a dictionary.

But it also looks like there's a strategy issue, where the code converts the original dictionary into separate lists of teachers and courses and assumes the index numbers will correspond. But dictionaries are orderless by definition, so it's never safe to make any assumptions about what order the keys or the values will appear in.

Using the challenge's suggestion of keeping track of a count as you iterate through the original dictionary would avoid this issue and probably be simpler to implement as well.

Anthony Grodowski
Anthony Grodowski
4,902 Points

Thanks! Will implement these advices asap and post the final code!

Anthony Grodowski
Anthony Grodowski
4,902 Points

But also look - I convert a dict to a list at the beggining of the def - "list_values = [v for v in dic.values()]", so the list remains the same trough the whole code. I don't really see the problem here that you pointed out. It pretty much does't matter in my opinion in what order they will come. Maybe I'm getting something wrong?

Steven Parker
Steven Parker
229,670 Points

Yes, you create a list from the values. Later, after you determine which set of values are the largest, you determine what index that set occupies in the list. Then, you use that index to access a name from another list that was independently created from the original dict. There's no guarantee that the order of the names list will be in the same order as the values list.

You could create both lists at the same time using dict.items(), but that's still more work than the strategy suggested by the challenge.

Anthony Grodowski
Anthony Grodowski
4,902 Points

I don't quite get where I used that index to access a name from another list. In teacher = (list(dic.keys())[list(dic.values()).index(list_values[0])]) I just search for the key corresponding to the greatest set of value. Also i don't understand what is max count variable...

Steven Parker
Steven Parker
229,670 Points

The list created by "list(dic.keys())" is a list of names, and is completely separate from the list of courses made with "list(dic.values())". Since both lists are made from an orderless collection, there's no guarantee that any correspondence between them will exist. So your search will always find something, but it may not be the right name for those courses.

And a "max count variable" would just be one where you keep track of the largest count seen so far. For example:

for teacher, courses in dic.items():
    if len(courses) > maxcount:
        maxcount = len(courses)
        #...
Anthony Grodowski
Anthony Grodowski
4,902 Points

I've Implemented your hints, again it returns right teacher name each time I test it, but still I get a bummer... What's wrong?

Steven Parker
Steven Parker
229,670 Points

You don't need to "split" the name, just return the teacher's whole name.

And while that will work, you can simplify the process a good bit by saving the "teacher" any time you update "maxcount". Then you have the answer as soon as the first loop finishes.