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

Teachers.py Task 4 of 4 - Please help me get to Tuples!

I'm not sure why it is only showing 5 values rather than 18

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(teacher_dict):
  max_count = 0
  for key, value in teacher_dict.items():
    if len(value) > max_count:
      max_count = len(value)
      max_teacher = key
  return max_teacher

def num_teachers(teacher_dict):
  teacher_count = 0
  for teacher in teacher_dict:
    teacher_count += 1
  return teacher_count

def stats(teacher_dict):
  teacher_list = []
  for teacher in teacher_dict:
    teacher_list.append([teacher, len(teacher_dict[teacher])])  
  return teacher_list

def courses(teacher_dict):
  course_list = []
  for courses in teacher_dict.values():
    course_list.append(courses)
  return course_list

1 Answer

Steven Parker
Steven Parker
229,644 Points

The error message is somewhat misleading. What you actually returned was not 5 courses, but 5 lists of courses.

All you need now is to iterate through the courses in each list and append each individual course to your master list:

def courses(teacher_dict):
  course_list = []
  for courses in teacher_dict.values():
    for course in courses:
      course_list.append(course)
  return course_list

OR you could just concatenate the lists of courses:

def courses(teacher_dict):
  course_list = []
  for courses in teacher_dict.values():
    course_list += courses
  return course_list

And I recently learned of a way to do the first method in a single line (though this is beyond what is covered in this particular course):

def courses(teacher_dict):
  return [course for courses in teacher_dict.values() for course in courses]

Thanks a million, Steven! Also, thank you for giving me a general rundown of what I needed to re-examine (to start your feedback) before giving me corrected code! It allowed me to come to this on my own without looking at your code.

  def courses(teacher_dict):\n  
      course_list = []\n  
      for courses in teacher_dict.values():\n  
          course_list.append(courses)\n  
          for course in courses:\n   
                 course_list.append(course)\n   
     del course_list[0:5]\n   
      return course_list\n   

Also, thank you for showing me multiple methods for solving and for the super concise one you recently learned!

I still need to learn Markdown.. sorry

Steven Parker
Steven Parker
229,644 Points

I normally avoid giving spoilers at all, but you were so close already plus it was an easy way to show the multiple approaches.

The trick to blockquotes in markdown is to skip a line, then put three accents ("backticks") on separate lines, and then your code in between. If possible, specify the language after the first set of accents, like this:

```py
def noop(something):
    """Do nothing here"""
    return something
```

and that produces this:

def noop(something):
    """Do nothing here"""
    return something

Thanks for the Markdown help, Steven! That will help a lot, both on here, and when I start using Github!