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

Yang Bi
Yang Bi
10,079 Points

got different result every time I tried to run this script

see attached of my script.

the function is meant to take a dictionary as an argument, list all items in the dictionary. so "countpair" should be a list of tupples. it then loop through the list and try to find out which teacher has the most course and return the teacher name.

this is the code I put after the script: print(most_courses({'lisa': ['python','sql','pythonbasic'], 'john': ['python','c','ee','c++'], 'jenny': 'asql','jacob':'python'}))

every time I run the code I got different results, "lisa" is most of my results, then "jenny" where the corrected answer should be "john", I do get john several times tho. I also sometime get a whole pair ex: ('lisa', ['python', 'sql', 'pythonbasic']) it seem like there is a timing issue in running the second for loop. anyone can explain this to me?? 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 most_courses(argument):
    countpair=[]
    for item in argument.items():
        countpair.append(item)
    teacher=countpair[0][0]
    for i in range(1, len(countpair)-1):
        if len(countpair[i][1]) > len(countpair[i-1][1]):
            teacher=countpair[i][0]
    return teacher

3 Answers

Hi, Yang: Perhaps it has something to do with how a regular dictionary doesn't hold on to the order of its key-value pairs, which means the position of the pairs inside the dictionary can change.

Anyway, this isn't the easiest of challenges... I forgot how I did this one when I took the course, so it took me quite awhile to figure out an answer... anyway, here's one way to do it:

def most_courses(arg):
    count_list = []
    for value in arg.values():
        count_list.append(len(value))
    dict_max = max(count_list)
    for key in arg.keys():
        if len(arg[key]) == dict_max:
            return key

Hope that helps! D.

Yang Bi
Yang Bi
10,079 Points

but the variable "countpair" is a list of pairs, the list should have orders right?

Yang Bi
Yang Bi
10,079 Points

ok, thanks for the answer, that actually makes more sense than mine. I was just too focused on the items() method, I didnt even think about the key() and value() method. Thank you very much.

You're welcome!!

p.s. if you took out the "-1" in your range function of your original answer, it would work : ) Range doesn't include the 'stop'.