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

Having problems with a challenge

In the last function "def courses()" I have to return all the courses in a single list, but the courses are in a list already, so I return many lists inside the list. How can I fix this?

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.

dict = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],  'Kenneth Love': ['Python Basics', 'Python Collections']}

def most_classes(aaa):

  count = 0
  the_teacher = ""

  for teacher in aaa:
    print(teacher)
    classes = len(aaa[teacher])
    if classes > count:
      count = classes
      the_teacher = teacher

  return the_teacher

def num_teachers(bbb):
  return len(bbb)

def stats(ccc):
  list = []
  for teacher in ccc:
    liste = []
    liste.insert(1, teacher)
    liste.insert(1, len(ccc[teacher]))
    list.insert(1,liste)
  return list

def courses(uuu):
  single_list = []
  for course in uuu:
    single_list.insert(1,uuu[course])
  return single_list


courses(dict)
most_classes(dict)
num_teachers(dict)
stats(dict)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The courses are held in the dictionary values. Using for course in uuu: will list the dictionary keys so "teacher" would be a clearer variable name. By using insert the entire list of courses for each teacher is added as a single element. Five teachers yields five inserted lists.

Instead of stepping through one list to add its elements to another list, you can use the extend() method:

def courses(uuu):
    single_list = []
    for teacher in uuu:
        single_list.extend(uuu[teacher])
    return single_list

Dictionaries can be iterated over using the .keys() (default), the .values(), or both using .items(). So this can be simplified by extracting the values directly in the for loop:

def courses(uuu):
    single_list = []
    for course_list in uuu.values():
        single_list.extend(course_list)
    return single_list

Clear and very well explained answer, thanks!

James Tink
James Tink
11,210 Points

So your problem is with this line:

    single_list.insert(1,uuu[course])

As you mentioned, right now you are inserting a list uuu[course] into another list single_list, which gives you a list of lists.

Can you think of a way to iterate the list uuu[course] and add each element of it individually to single_list? My hint is that it is very similar to how you iterate the dictionary:

for course in uuu: