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

Anders Axelsen
Anders Axelsen
3,471 Points

Task 3 of 5: How do I prevent my code from returning a list of a list?

I get this error: Bummer! Did you return a list of lists? I need just a single list.

  • Note for developers: It can be hard to research debugging, when both order of tasks and function names have changed over time.
teachers.py
# The dictionary will look something like:

#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.


def num_courses(dict1):
    count = 0
    for i in dict1:
        list1 = dict1[i]
        for i in list1:
            count += 1
    return count 

def num_teachers(teachers_dict):
    return len(teachers_dict)

def courses(dictus):
    allCourses = []
    for courses in dictus.values():
        for course in courses:
            allCourses.extend(dictus.values())
    return allCourses

2 Answers

Hi Anders,

I think the second for loop is causing the problem here. I'll check now, but I think your code is fine if you just omit that loop.

[EDIT] The second loop is unnecessary but you're appending the wrong thing to your list. dictus.values() is a list. You want to extend the allCourses list with courses:

def courses(dictus):
    allCourses = []
    for courses in dictus.values():
         allCourses.extend(courses)
    return allCourses

Steve.

Anders Axelsen
Anders Axelsen
3,471 Points

Hi Steve!

Thank you - what a relief. It works with exending the allCourses by the means of (courses).

Kind regards, Anders.

No problem! :+1: :smile: