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

kirkbyo
kirkbyo
15,791 Points

Dictionaries [4/4] Error, Although Code works in workspaces

I keep running into an error on the 4th task of this challenge. The question

Write a function named courses that takes the dictionary of teachers. It should return a list of all of the courses offered by all of the teachers.

My Code

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

def courses(teacher_dict):
  allCourses = []
  for courses in teacher_dict:
    tempCourse = []
    tempCourse.append(teacher_dict[courses])
    allCourses.append(tempCourse)
  return allCourses

courses(teachers)

After I run this code in the challenge, I get an error saying that I returned 5 values instead of 18. Although when I run the same code in workspaces, everything runs fine and all the objects from the 'teacher' dictionary gets returned.

Why is this happening?

Ozzie

1 Answer

Santos Solorzano
Santos Solorzano
3,125 Points

Hmm, I can't quite help you out with your error, BUT I ran your code on my Mac and got this:

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

Try using extend() instead of append() !!!

append adds an element to a list, extend concatenates the first list with another list!

kirkbyo
kirkbyo
15,791 Points

Thank you! I ended using a different method to originally pass the challenge, but changing it from append to extend did end up working.

def courses(teacher_dict):
  allCourses = []
  for courses in teacher_dict:
    tempCourse = []
    tempCourse.extend(teacher_dict[courses])
    allCourses.extend(tempCourse)
  return allCourses

Thanks a ton,

Ozzie

Dean McKenzie
Dean McKenzie
11,149 Points

Thanks Santos, the extend() worked out great!