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 trialKafe Hezam
11,070 Pointsappending a string into a list
Why when I try to use the append() method in this case, it does not let pass?
def courses(single):
new_list = []
for course in single.values():
new_list.append(course)
return new_list
# 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(single):
return len(single)
def num_courses(single):
courses = 0
for course in single:
courses += len(single[course])
return courses
def courses(single):
new_list = []
for course in single.values():
new_list.append(course)
return new_list
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsThe course
values are lists. So appending a list to a list using the append()
method will append the entire list of courses as a single item. This ends up looking like:
new_list = [
['jQuery Basics', 'Node.js Basics'],
['Python Basics', 'Python Collections']
]
If you wish to append all the items in a list to another list, use the extend()
method instead.
Post back if you need more help. Good luck!!!
Kafe Hezam
11,070 PointsYeah that helped a lot! Than you so much