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 (Retired) Dictionaries Teacher Stats

What's the expected output for task 3 of the the Python collections challenge?

The specs are to create a function that accepts a dictionary and returns a list in the form (<code><</code>name<code>></code>, <code><</code>number of classes<code>></code>). Have tried multiple ways to no avail.

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
def most_classes(dict_teachers):
  max_count = 0
  teach_max = ""
  for teach in dict_teachers:
    if len(dict_teachers[teach])> max_count:
        max_count = len(dict_teachers[teach])
        teach_max = teach
  return teach_max

def num_teachers(dict_two_teach):
  return len(dict_two_teach)

def stats(dict_three_teach):
  for teach in dict_three_teach:
    return (teach, len(dict_three_teach))

[Edit: formating --cf]

1 Answer

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Eliot!

The desired output is a list of list, so 1 list for each teacher and inside this list we want (at least they want) the name of the teacher and the number of courses he has.

The is a very handy method in python for accessing the items in the dictionary that is the .items() method. So, something like:

for teacher,courses in dict_three_teach.items():

will give you both the teacher and his relative courses. We can then use the teacher as it is and calculate the number of courses using the length of the "course" list we got back.

Please also note that you may want to initialize or more variables before running the loop!

I hope this helps, if still stuck, please ask!

Vittorio