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

Teacher Stats Exercise 1- Problem-how to return the key

Hi,

Sorry for asking so much but how can I return the key fitting to the found value. You can see my code here:http://pastie.org/private/us8lcvr4rzoi6wqrcg4kfa The others methods don't work for me.

Thanks in Advance for help and Sorry for those many questions.

3 Answers

Steven Parker
Steven Parker
229,670 Points

One way to do it would be to loop over the dictionary items directly (not just the values), and when you update the max_count, also save the key that goes with it. Then return the key when the loop is over.

For future postings, remember to include a link to the challenge page. And for info on including your code in your posting, see the Markdown Cheatsheet pop-up below the answer area. :arrow_heading_down:

This is my code. Maybe you can help me to bring it on the right way, so I can learn the most from it.

def most_classes (dict_of):
    max_count=0
    for value in dict_of.values():
        if len(value)>max_count:
            max_count=len(value)
        return #key that fits to the value, this is my stucking point.

Thanks in Advance;)

Aby Abraham
PLUS
Aby Abraham
Courses Plus Student 12,531 Points
def most_classes(dict):
    max_count = 0
    str = ""
    alist = dict.values()
    for key, value in dict.items():
        if len(value) > max_count:
            max_count = len(value)
            str = key
    return str

Thank you very much!