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

Idan shami
Idan shami
13,251 Points

help me understand something... task 3 teacher stats

hi (: I am having some problems with this task.... when I am trying the code in work spaces it gives me 5 lists and not all of the values in 1 list, so I tried looking at others answer and I found this one

def courses(a_dict):
    course_list = []
    for teachers in a_dict:
        for courses in a_dict[teachers]:
            course_list.append(courses)
    return course_list

why is he using 2 loops? and what a_dict[teacher] does that affect the list to be 1...? can I do it with the .value() statement?

I really appreciate your help! thanks!

Idan.

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.
teachers_course = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],'KennethLove': ['Python Basics', 'Python Collections']}
def num_teachers(teachers_course):
    count = 0
    for teacher in teachers_course:
        if teacher in teachers_course:
            count += 1
        else:
            count = 0
    return count

def num_courses(teachers_course):
    count_course = 0
    for course in teachers_course.values():
        if course in teachers_course.values():
            count_course += len(course)
        else:
            count_course = 0
    return count_course

def courses(teachers_course):
    values_list = []
    for value in teachers_course.values():
        values_list.append(value)
        return values_list
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.

# 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.
teachers_course = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],'KennethLove': ['Python Basics', 'Python Collections']}
def num_teachers(teachers_course):
    count = 0
    for teacher in teachers_course:
        if teacher in teachers_course:
            count += 1
        else:
            count = 0
    return count

def num_courses(teachers_course):
    count_course = 0
    for course in teachers_course.values():
        if course in teachers_course.values():
            count_course += len(course)
        else:
            count_course = 0
    return count_course

def courses(teachers_course):
    values_list = []
    for value in teachers_course.values():
        values_list.append(value)
        return values_list

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,453 Points

In your attached code snippet:

def courses(a_dict):
    course_list = []
    for teachers in a_dict:  # <-- teacher is each key in a_dict
        for courses in a_dict[teachers]:  # <-- a_dict[teachers] will be the list of courses
                                          # courses will be each course in the list of courses
            course_list.append(courses)  # <-- append course to course_list
    return course_list

But, since a_dict[teachers] is a list already, it can be added to the courses_list directly and skip the inner for loop with

    course_list.extend(a_dict[teachers])

Your posted code, is very close to the solution!

def courses(teachers_course):
    values_list = []
    for value in teachers_course.values():
        values_list.append(value) # <-- use extend instead of append otherwise you'll get a list of list
        return values_list  # <-- return value indented too far

Post back if you need more help. Good luck!!