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

teachers.py help, not sure why this isn't returning a list

def courses(dictionary):
    return list(dictionary.values())

courses is supposed to take a dictionary and return a list of all possible values from the dictionary. I've tried this code in a shell and it appears to work. The error message the treehouse assignment is giving says: Did you return a list of lists? I need just a single list.

Any and all help appreciated. Thanks!

1 Answer

andren
andren
28,558 Points

As the error message states, your code will return a list of lists, not a list that contains just the courses as strings.

With the example dictionary shown in the challenge your function would return:

[['jQuery Basics', 'Node.js Basics'], ['Python Basics', 'Python Collections']] # list with 2 lists

When the value it is supposed to return is this:

['jQuery Basics', 'Node.js Basics', 'Python Basics', 'Python Collections'] # list with 4 strings

In order to get a flat list like that you have to loop through dictionary.values() and add the contents of the lists it returns to a list that you return.

Like this:

def courses(dictionary):
    all_courses = []
    for course_list in dictionary.values():
      all_courses += course_list
    return all_courses