append
and extend
are list methods in Python which can be used to combine multiple lists. But what is the difference between them? When should you use one over another, let’s find out.
The official documentation describes them as:
list.append(x)
: Add an item to the end of the list; equivalent to a[len(a):] = [x]
.
list.extend(L)
: Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L
.
Notice the bold words in the definition. append adds an item
and extend appends all the items of the given list
.
Let’s take an example:
some_list = ['string', 1, 'another string', 6.5]
another_list = ['new data', 12]
some_list.append(another_list)
print some_list
Output:
['string', 1, 'another string', 6.5, ['new data', 12]]
As you can see, the append
method took an item
i.e. the object of another list and added it to some_list
as it is.
some_list = ['string', 1, 'another string', 6.5]
another_list = ['new data', 12]
some_list.extend(another_list)
print some_list
Output:
['string', 1, 'another string', 6.5, 'new data', 12]
In case of extend
you can see that all the items from another_list
were appended to some_list
one by one. So you can say extend method concatenates one list with another list.
Q: What will be the output of below code:
some_list = ['string', 1, 'another string', 6.5]
data = 'hello'
some_list.extend(data)
print some_list
A: Output is
['string', 1, 'another string', 6.5, 'h', 'e', 'l', 'l', 'o']
Explanation: If you notice carefully, data
is a string and strings in Python are iterable
. Therefore the extend
method iterated over all characters in the string one by one and appended them to the list.
So based on above we can say that below 2 code snippets are equivalent:
for data in iterator:
some_list.append(data)
some_list.extend(iterator)
Help us improve this content by editing this page on GitHub