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 trialMit Sengupta
13,823 PointsI keep wondering why am I getting 4.
I think I have got it all right but I do not know why I'm failing to return all the dictionaries. Please help me someone. Thanks.
Here's the code:
def courses(teachers):
output = []
for courses in teachers.values():
output = courses
return output
[MOD: added ```python formatting -cf]
3 Answers
Chris Freeman
Treehouse Moderator 68,454 PointsIt looks like you're combining solutions for task 3 and task 4. Your original posted code was very close to the solution. Change the output
assignment to use .extend()
:
# Task 4 of 4
# Great work! Finally, write a function named 'courses' that takes the
# teachers dictionary. It should return a single list of all of the
# courses offered by all of the teachers.
def courses(teachers):
output = []
for courses in teachers.values():
output.extend(courses) # <-- changed output from assignment to 'extend'
return output
Chris Freeman
Treehouse Moderator 68,454 PointsIn many cases, the issue is using .append()
to create the list of all courses. The solution usually involves using .extend()
so you end up with one list of items instead of a "list of lists" result.
Mit Sengupta
13,823 PointsThis is my current modified code :
def stats(dicts):
course = []
for courses in dicts:
course.extend(courses)
return course
and now am getting this error: Bummer! Didn't get the expected output. Got ['Andrew Chalkley']
[MOD: added ```python formatting -cf]
Jason Whitaker
12,252 Pointsin your for loop you are just getting the key. it needs to be the key and a count of the values in the list.
for key in dicts:
add_to_course_list = [key, len(dicts[key])
course.extend(add_to_course_list)
return course
Chris Freeman
Treehouse Moderator 68,454 PointsTo access the courses, you can use the .items()
method
for courses in dicts.items():
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsCan you post your current code?