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<noob />
17,062 PointsWhy 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
231,060 PointsWhen 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" [ 1, 2, 3, [4, 5, 6] ] (4 items)
- using "extend" [ 1, 2, 3, 4, 5, 6 ] (6 items)
<noob />
17,062 Points<noob />
17,062 PointsThanks for ur time to answer my question! :D