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

Rabih Atallah
Rabih Atallah
3,405 Points

Hi please any ideas for this challenge:

help is much appreciated

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(teachers):
  G = []
  max_count = 0
  for k in teachers.keys():
    max_count = len(teachers[k])
    G.append(max_count)
  for i in teachers.keys():
    if len(teachers[i]) == max(G):
      return i

def num_teachers(teachers):
  L = []
  for t in teachers.keys():
    L.append(t)
  return len(L)

def stats(teachers):
  L = []
  for i in teachers.items():
    L.append(list(i))
  return L

5 Answers

My preference when doing this was to loop over each key in the dictionary using the loop

for key in dictionary:
     #do something here

Since the key in this case is the name of the teacher. From there you can construct the list to add and add it inside the loop.

Hello,

What this Challenge is asking you to create the function stats which takes in a dictionary, and then returns a list where each element in the list is a list in the format of [name, num_of_courses]

Rabih Atallah
Rabih Atallah
3,405 Points

please, any hints on how to start the script. The method: k,v in items.dict(): is returning a tuple

Rabih Atallah
Rabih Atallah
3,405 Points

I tried the loop but its hard ti build a list with 2 items (key, value) with each step. any hints pleas?

You can add a list as an element of a list by doing:

list.append([item1, item2, item3, ... , itemX])

where list is the name of your list of lists and the items are the items you want to add to the list element

Rabih Atallah
Rabih Atallah
3,405 Points

Thank you James! it worked, that was helpful