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

Adam Oldenkamp
Adam Oldenkamp
1,778 Points

my courses functions returns 5 courses, which, when looking at the example list makes sense, why does it excpect 18?

There is clearly more to this problem, but I can't figure out what the next step is.

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!

def most_classes(teach_dict):
  max_count = 0
  teacher_name = ''
  for key in teach_dict.keys():
    name = key
    classes = len(teach_dict[key])
    if classes > max_count:
      max_count = classes
      teacher_name = name
  return teacher_name

def num_teachers(teach_dict):
  count = 0
  for key in teach_dict.keys():
    count += 1
  return count


def stats(dicts):
    stats_list = []
    for key, value in dicts.items():
      classes = int(len(value))
      item  = [key, classes]
      stats_list.append(item)
    return stats_list

def courses(new_dict):
  course_list = []
  for key, value in new_dict.items():
    course_list.append(value)
  return course_list
Adam Oldenkamp
Adam Oldenkamp
1,778 Points

Replacing .append with .extend solved my issue, but I'm not entirely sure why.

1 Answer

Hanley Chan
Hanley Chan
27,771 Points

Hi,

It will work if you use course_list.extend(value) or concatenate value to course_list with a '+' instead of course_list.append(value). Append is inserting the whole value array into course_list array instead of only the array elements.

If you used append in the example dictionary would get:

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

instead of:

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

def courses(new_dict):
  course_list = []
  for key, value in new_dict.items():
    course_list.extend(value)
  return course_list

Hope this helps.