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

Deniz Kahriman
Deniz Kahriman
8,538 Points

What is wrong here? Trying to create a single list from the values of a dictionary by avoiding duplicates. Thanks!

I'm getting an error saying didn't get all of the courses :/ Thank you in advance!!

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(teachers_courses):
    return len(teachers_courses)
def num_courses(teachers_courses):
    list_courses = []
    for courses in teachers_courses.values():
        list_courses.extend(courses)
    return len(list_courses)
def courses(teachers_courses):
    all_courses = []
    for values in teachers_courses.values():
        for i in values:
            if i not in all_courses:
                all_courses.extend(i)
            else:
                all_courses = all_courses
    return all_courses

2 Answers

nicole lumpkin
PLUS
nicole lumpkin
Courses Plus Student 5,328 Points

Hello Deniz,

Don't look at code block if you want to re-try on your own!

I think you may be misunderstanding the objective, which is to return a list of all of the courses taught by all of the teachers. There is no mention of avoiding duplicates, therefore you can simply append (that's what I did, course by course), or perhaps extend (thanks for the idea, might save me a line of code!) to all_courses. Also, the else clause is not necessary. I've posted my code below, so If you don't wish to see it stop reading now!!!

def courses(teacher_courseNames):
    course_list = []
    for value in teacher_courseNames.values():
        for course_name in value:
            course_list.append(course_name)
    return course_list

I wonder if the following would be more crisp:

def courses(teacher_courseNames):
    course_list = []
    for value in teacher_courseNames.values():
        course_list.extend(value)
    return course_list
Deniz Kahriman
Deniz Kahriman
8,538 Points

Thanks, Nicole!! I tend to assume the challenges are more complicated than they actually are :) You are correct, I didn't have to look for duplicates.