Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,012 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!