Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

Why i cant use this method? enter to understand my question

In part 3 of this challnage i tried this solution first:

def courses(num_courses):
    list_courses = []
    for x in num_courses.values():
        list_courses.append(x)
    return list_courses

and somehow it didnt passed, and it should have worked because the APPEND method is appending an element to the end of the list

i saw a solution from alex davison and he uses the EXTEND method instead of the APPEND method, anyone can explain to me what is the difference here?

this is the code that passed:

def courses(num_courses):
    list_courses = []
    for x in num_courses.values():
        list_courses.extend(x)
    return list_courses



thanks in advance ;D

```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):
    return len(teachers)

def num_courses(courses):
    number = []
    for course in courses.values():
        number += course
    return len(number)

def courses(num_courses):
    list_courses = []
    for x in num_courses.values():
        list_courses.extend(x)
    return list_courses

1 Answer

Steven Parker
Steven Parker
216,136 Points

When you use "append" to add a list to another list, the entire list is added as a single item. But when you use "extend", each element is added individually.

For example, if you were to combine a list containing [1, 2, 3] and one with [4, 5, 6]:

  • using "append" :point_right: [ 1, 2, 3, [4, 5, 6] ]   (4 items)
  • using "extend" :point_right: [ 1, 2, 3, 4, 5, 6 ]   (6 items)

Thanks for ur time to answer my question! :D