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 trialMathew Yangang
4,433 Pointssingle list
I'm having issues returning a single list from task 3 . My code wouldn't pass
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
# 'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.
def num_teachers(teacher):
number = len(teacher)
return number
def num_courses(teacher):
course = 0
for teachers in teacher.values():
course += len(teachers)
return course
def courses(teachers):
for course in teachers.values():
list = [].extend(course)
return list
1 Answer
Alexander Davison
65,469 PointsYou on the right track!
It's a good idea to iterate through the values of the dictionary, and to use .extend()
But you might have to change things up a little.
The code you write will always return None
, because .extend()
actually returns nothing. .extend()
extends in place the list you call it on, and it does not return anything. For example:
Pretend I'm in the Python Shell
>>> my_list = [1, 2, 3]
>>> print([1, 2, 3].extend([4, 5, 6]))
None
>>> print(my_list.extend([4, 5, 6]))
None
>>> print(my_list)
[1, 2, 3, 4, 5, 6]
So, instead, you may want to write something like this:
def courses(teachers):
all_courses = []
for teacher_courses in teachers.values():
all_courses.extend(teacher_courses)
return all_courses
Mathew Yangang
4,433 PointsMathew Yangang
4,433 PointsThanks Alexander, that was so helpful
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsYou are very welcome