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

frank sebring
frank sebring
1,037 Points

Question 3 of Challenge Four: I don't understand what to do on this question could someone please give me some advice

Great work! OK, you're doing great so I'll keep increasing the difficulty. For this step, make another new function named courses that will, again, take the dictionary of teachers and courses. courses, though, should return a single list of all of the available courses in the dictionary. No teachers, just course names!

if i use the command it does not work. This method in a complier will return (print if I use print instead of return. I understand the difference in the two and just do that to see the output.) the values but they are in two list.

print(list(treehouse.values()))

dict_values([["Python Basics", "Python Collection"], ["jQuery Basics", "Node.js Basics"]])

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(treehouse):
    return(len(treehouse.keys()))

def num_courses(treehouse):
    return(sum(len(x) for x in treehouse.values()))

def courses():
    return(treehouse.values())

3 Answers

You need to add up all of the lists.

Also, where did the treehouse parameter go?

Maybe try something like this?

def courses(treehouse):
    courses = []
    for x in treehouse.values():
        courses.extend(x)
    return courses
Eric Harris
Eric Harris
2,391 Points
def courses(dictionary):
    r = []
    for x in dictionary.values():
        for z in x:
            r.append(z)
    return r

function to return a LIST containing values from a dict.

def courses(dict):

create a new empty list

list_courses = []

iterate through dict to get 'values'(courses)

for value in dict.values():

Take those 'values'(courses) received and append to the empty list

    for val in value:
        list_courses.append(val)

Your return statement here

This is a basic template to the objective, Hopefully this helps to get your head wrapped around it.