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

function names courses

It says it only returned 5 courses but there should be 18. Any assistance 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(my_dict):
  max_count=0
  busy_teacher=''
  for x,y in my_dict.items():
    if len(y)>max_count:
      max_count=len(y)
      busy_teacher=x
  return busy_teacher

def num_teachers(my_dict):
  return len(my_dict)

def stats(my_dict):
  teacher_list=[]
  for key in my_dict:
    my_list=[]
    my_list.append(key)
    my_list.append(len(my_dict[key]))
    teacher_list.append(my_list)
  return teacher_list

def courses(my_dict):
  courses_list=[]
  for key in my_dict:
    my_course=[]
    my_course.append(key)
    my_course.append(len(my_dict[key]))
    courses_list.append(my_course)
  return courses_list

2 Answers

Dan Johnson
Dan Johnson
40,533 Points

The courses_list doesn't need to contain the name of the teacher, so you won't have to append the key. All the list needs to contain is the course names.

For an outline:

# Initialize an empty list
# For every value in the dictionary
#     Extend the list with that value
Ryan Merritt
Ryan Merritt
5,789 Points

Here's what the python interpreter outputs when you run your courses() function.

>>> my_dict
{'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections']}
>>> 
>>> 
>>> def courses(my_dict):
...   courses_list=[]
...   for key in my_dict:
...     my_course=[]
...     my_course.append(key)
...     my_course.append(len(my_dict[key]))
...     courses_list.append(my_course)
...   return courses_list
... 
>>> 
>>> 
>>> result = courses(my_dict)
>>> result
[['Jason Seifer', 3], ['Kenneth Love', 2]]

As you can see, the function creates a list of lists, each containing the teacher's name and the number of courses they teach. Our goal is to return a list of all the courses taught (with no repeats!) Since we are only concerned with the values of the dictionary, and not with the keys (the names of the teachers), we can use my_dict.values()

This will return a list of all values, which are themselves a list. So like...

[['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], ['Python Basics', 'Python Collections']]

We could loop through my_dict.values() then loop through each value...

all_courses = []
vals = my_dict.values()
for course_list in vals:
    for course in course_list:
        if course not in all_courses:
            all_courses.append(course)

OR we could get fancy with comprehensions :D

def courses(my_dict):
    vals = my_dict.values()
    return list({ course for course_list in vals for course in course_list })

This may look confusing at first, but if you compare with the code above, you will see that the nested for loops are simply listed one after another (for course_list in vals followed by for course in course_list). I used a set comprehension to avoid duplicate course titles, so we have to convert it into a list using list()

The comprehension could arguably be less readable, so maybe the conciseness isn't worth it, but it's fun.