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

Challenge Task 4 of 5 Create a

Here is another one. I think the challenges are too forward compare to PHP and other courses

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(teachers):
    count = 0
    for teacher in teachers:
        count += 1
    return count

def num_courses(teachers):
    count=0
    for course in teachers.values():
        count += len(course)
    return count
def courses(d):
    course=[]
    for e in d.values():
        course.extend(e)
    return course

def most_courses(teachers):
    for value in len(teachers.values()):
        print (keys(max(value)))

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Try this approach:

  • For each value in teachers.values()
  • if length of value is greater than current max (initialized to 0),
    • then set new max value, and
    • save name of teacher as potential result
  • return last saved teacher name

Post back if you need more hints. Good luck!

Thomas Bråten
Thomas Bråten
1,408 Points

Im struggling to understand how I can get the key? Which is the teachers name if I get it right. So I get back lists of the courses, but how would I go about getting the keys again and connecting it with the values here. Hmm, the first three was a breeze, but the more I think of this one, the more confused I get. Sorry if I can't word this better, but I'm just confused. Hope someone can help explaining. I also looked up for key, value in dict.items(): , not sure if something maybe can be done that way instead. Confused :/

def most_courses(arg):
    max_count = 0
    for value in arg.values():
        if len(value) > max_count:
            max_count += 1
            print (value)
            print(type(value))
Mike Stanton
Mike Stanton
Courses Plus Student 3,250 Points

Thomas,

You would need to iterate over the teachers dictionary, for example: for value in arg.values(): only stores the values in value from the dictionary. If you you args.keys() it will store keys. to iterate over the args and pull back keys and values you should use the args.items() for key, value in args.items(): Then you can use the keys/values stored to add more logic to your loop

Hope this some helps. Mike-