Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

What is the difference between Python's list methods append and extend?

What's the difference between the list methods append() and extend()?
by

2 Answers

kshitijrana14
append: Appends object at the end.
x = [1, 2, 3]
x.append([4, 5])
print (x)

gives you: [1, 2, 3, [4, 5]]
extend: Extends list by appending elements from the iterable.
x = [1, 2, 3]
x.extend([4, 5])
print (x)

gives you: [1, 2, 3, 4, 5]
MounikaDasa
append-adds its argument as a single element to the end of a list. The length of the list itself will increase by one.
extend-iterates over its argument adding each element to the list, extending the list. The length of the list will increase by however many elements were in the iterable argument.

Login / Signup to Answer the Question.