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

Sahar Nasiri
Sahar Nasiri
7,454 Points

What is going on?

According to the sample dictionary I get 5 courses from the code below, but I get an error that I need to have 18 courses!!! HOW??? There is only 7 words in that dict!!!

for value in dict_of_teachers.values(): if value not in list_of_courses: list_of_courses.append(value) else: continue return list_of_courses

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_of_teachers):
  max_count = 0
  max_teacher = ""
  for key in dict_of_teachers:
    if len(dict_of_teachers[key]) > max_count:
      max_count = len(dict_of_teachers[key])
      max_teacher = key
    else:
      continue
  return max_teacher

def num_teachers(dict_of_teachers):
  return len(dict_of_teachers)

def stats(dict_of_teachers):
  list_of_list = []

  for key in dict_of_teachers:
    list_of_list.append([key, len(dict_of_teachers[key])])
  return list_of_list

def courses(dict_of_teachers):
  list_of_courses = []

  for value in dict_of_teachers.values():
    if value not in list_of_courses:
      list_of_courses.append(value)
    else:
      continue
  return list_of_courses 

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Each value in the dict_of_teachers is a list. When you use append the entire list of a teacher is a appended. 5 teachers, 5 lists.

Instead use extend

    list_of_courses.extend(value)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

The example data may differ from the data used by the challenge grader.

reza sajadian
reza sajadian
718 Points

Yeah. The same strange problem! Thanks.