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

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

most_courses challenge

Not sure how to access the dictionary keys to return the teacher in the for loop if we're using the for loop on the dictionary values.

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(dict_teachers):
    count=0
    for teacher in dict_teachers.keys():
        count+=1
    return count    
def num_courses(dict_courses):
    count=0
    for courses in dict_courses.values():
        count+=len(courses)
    return count 
def courses(course):
    course_list=[]
    for title in course.values():
        course_list+=title
    return course_list
def most_courses(courses):
    count=None

    for title in courses.values:
        if len(title)>count:
            count=len(title)
        else:
            continue
        return

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Here's how I would loop the courses and know the teacher:

test_dict = {
    'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
    'Kenneth Love': ['Python Basics', 'Python Collections']
}

for name, course_list in test_dict.items():
    print('\n')
    print('Name is --> ', name)
    print('Course List is --> ', course_list)
    for course_name in course_list:
        print('Individual course ', course_name, 'for teacher ', name)

# output

Name is -->  Andrew Chalkley
Course List is -->  ['jQuery Basics', 'Node.js Basics']
Individual course  jQuery Basics for teacher  Andrew Chalkley
Individual course  Node.js Basics for teacher  Andrew Chalkley


Name is -->  Kenneth Love
Course List is -->  ['Python Basics', 'Python Collections']
Individual course  Python Basics for teacher  Kenneth Love
Individual course  Python Collections for teacher  Kenneth Love

Is that what you're asking? :palm_tree:

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

Thanks, this solves how I can include the keys in the for loop using the items() method but I need to find the teacher with the most courses. I updated my code with the items method but now I'm getting an error saying I didn't get the right teacher.

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Correct, the purpose was to answer

how to access the dictionary keys to return the teacher in the for loop

Sounds like you are having trouble debugging your logic to determine the teacher with the most courses and to return that teacher's name in the required format. What's your code look like and what do you get when testing it in a workspace or locally?

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

It's ok, I finally figured it out. My return shouldn't have been in the for loop. Thanks!