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
Md Akibe Hashan
2,422 PointsI didn't understand the main difference bitwen append and extend
.append(<value>) - Add a new value onto the end of a list.
.extend(<iterable>) - Make a list longer by adding on the members of another iterable.
.insert(<index>, <value>) - Add a value to a list at a particular index.
You can, of course, also add lists together with the + operator. To do this in place, you'd use the increment operator +=.
1 Answer
Steven Parker
243,266 PointsThe difference is easier to see when you add one list to another. "Append" adds the entire new list as one item (creating a list inside a list), but "extend" adds each item of the new list individually. For example:
lista = [1, 2, 3]
listb = [1, 2, 3]
new = [4, 5, 6]
lista.append(new) # <- this gives you [ 1, 2, 3, [4, 5, 6] ]
listb.extend(new) # <- this gives you [ 1, 2, 3, 4, 5, 6 ]