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 (2016, retired 2019) Dictionaries Teacher Stats

single list

I'm having issues returning a single list from task 3 . My code wouldn't pass

teachers.py
# 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

You 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

Thanks Alexander, that was so helpful

You are very welcome :wink: