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

Craig Campbell
Craig Campbell
14,428 Points

can't seem to get the list of lists correct....

I could get it to make a list of lists like this: [ [name 1, name2, ...] , [number of classes 1, number of 2, ....]

But I couldn't get it to do [ [ name 1, class 1] , [name 2, classes 2], ...]

Can someone check my code? I need a hint here...

stats(teacher_dict)

teachers.py
#!/usr/bin/env python3
'''
Create a function named most_classes that takes a dictionary of teachers
and returns the teacher with the most classes.
'''
# 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):
  max_count = 0
  most_teacher = ""
  for key in dictionary:
   if len(dictionary[key]) > max_count:
    max_count = len(dictionary[key])
    most_teacher = key

  return most_teacher


'''
Create a function named num_teachers that takes a dictionary of teachers
and returns the number of teachers.
'''

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

  return teacher_count




''' create a function named stats that takes a dictionary of teachers and
returns a list of lists in the format [<name>, <number of classes>]
'''


def stats(dictionary):
  name_list = []
  class_count_list = []
  final_list = []

  for name in dictionary:
    name_list.append(name)
    class_count_list.append(len(dictionary[name]))



  final_list.append(name_list)
  final_list.append(class_count_list)


  return final_list


# I could get it to make a list of lists like this:  [ [name 1, name2, ...] , [number of classes 1, number of  2, ....]
# But I couldn't get it to do [ [ name 1, class 1] , [name 2, classes 2], ...]


# Can someone check my code?  I need a hint  here...
# stats(teacher_dict)

2 Answers

Hanley Chan
Hanley Chan
27,771 Points

Hi,

For your final list you ended up with what you got because you are appending your class_count_list to the end of your name_list.

Here is how I solved this one. I changed the for loop to append [name, #classes] to final_list

 for name in dictionary:
    final_list.append([name, len(dictionary[name])]) 

return final_list
Craig Campbell
Craig Campbell
14,428 Points

wow. Thanks, Hanley! That was an amazingly simple fix! Gracias!