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 trialAlan Temirov
Courses Plus Student 762 PointsWhy it doesn't let me...
In the task after the video where it asks me to add name "Paul" to the beatles list it doesn't let me to do it through extend method - beatles.extend(["Paul"]), I can only add it through append method, but when I go and try it into the python shell the result turns out to be the same for both methods, are there any subtle details that I don't know?
1 Answer
Josh Keenan
20,315 PointsThey are different, extend()
works by iteration whereas append()
just adds something to the end of a list.
append()
will work by adding whatever you pass it to the end of a list, meaning if you past a list it will add that list to the end of your current one.
my_list = ['a', 2, 'hello']
my_list.append(['b', 3, 28])
Your list now looks like this:
['a', 2, 'hello', ['b', 3, 28]]
extend()
will iterate over whatever you pass it and add them in individually to the list:
my_list = ['a', 2, 'hello']
my_list.append(['b', 3, 28])
Now your list looks like this:
['a', 2, 'hello', 'b', 3, 28]
Hope this helps!