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

I have a list of lists stating the teacher and the number of classes but it keeps saying it didn't get what is expected.

No idea what is wrong with it. It looks perfect.

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(dictionary):
  the_most = 0
  for key,value in dictionary.items():
    teacher_classes = len(value)
    if teacher_classes > the_most:
      the_most = teacher_classes
      most = key
      continue
    else:
      continue
  return(most)

def num_teachers(dictionary):
  teachers = 0
  for value in dictionary:
    teachers += 1
  return teachers

def stats(dictionary):
  list_of_lists = []
  for key, value in dictionary.items():
    length = int(len(value))
    string = "{},{}".format(key, length)
    string = string.split(",")
    list_of_lists.append(string)
  return(list_of_lists)

1 Answer

Dan Johnson
Dan Johnson
40,533 Points

The number of classes in each sub list is expected to be of type int. You could convert after the split:

def stats(dictionary):
  list_of_lists = []
  for key, value in dictionary.items():
    length = int(len(value))
    string = "{},{}".format(key, length)
    string = string.split(",")
    # Change string[1] back to an int
    list_of_lists.append([string[0], int(string[1])])
  return(list_of_lists)

However there's no need for format strings. Using your existing code you can reduce it down:

def stats(dictionary):
  list_of_lists = []
  for key, value in dictionary.items():
    list_of_lists.append([key, len(value)])
  return list_of_lists