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

Christian Rowden
Christian Rowden
2,278 Points

Code works but need direction regarding how to record it properly.

The code seems to work correctly in that it records the correct number...My real frustration lies in my limited ability to express myself in python thus far. How can I record a key in relation to the value that is recorded for max_value??

If I could just get a push in the right direction I would greatly appreciate it. I'm fighting the urge to look the answer up in the forums.

Thank you very much.

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(arg):
    return len(arg)

def num_courses(arg):
    return sum(len(v) for v in arg.values())

def courses(arg):
    single_list = []
    for v in arg.values():
        for items in v:
            single_list.append(items)
    return single_list

def most_courses(arg):
    new_dict = {}
    max_value = 0
    swap_value = 0
    for v in arg.values():
        for items in v:
            swap_value += 1
            if swap_value > max_value:
                max_value = swap_value
                swap_value = 0
    return max_value

Do you want to create a dictionary that has a key and max_value? Could you please clarify it more?

2 Answers

Christian Rowden
Christian Rowden
2,278 Points

Not really... This just looks horrific and extremely unpythonic but I don't know how to express my solutions that well yet in python.

So the code snippet at the bottom is unpacking and recording the max_value of each value if the iteration that follows is greater than the previous one. Now that I have the correct iteration stored in max_value, how to I associate the key that goes to that particular value?

Christian Rowden
Christian Rowden
2,278 Points

Please disregard this.. I believe that I've come to a solution. I was getting tunnel vision on the d.values().