There are two methods in Python that can add an element to a list: append and extend. These methods can cause a lot of confusion among beginner Python programmers.
Difference between append and extend
append
This method adds a single element to the list. If you have another list with objects, the append method will increase the size of the list by one. It doesn’t matter how many objects the second list has.
extend
This function iterates through its arguments and adds each of them at the end of the list. The length of the first list will increase by the number of elements added from another list.
append and extend for non-iterable objects
append
The append method can work both with iterable objects and non-iterable objects. non-iterable objects are objects that you can’t access using an index, such as int, float, etc.
This code will return an error.
1 2 |
my_int = 455 print(my_int[0]) |
It means that the object is not iterable.
In this code, there is a list (list_append) and a non-iterable int object (my_var). This code works perfectly well.
1 2 3 4 5 |
list_append = [1, 2, 3] my_var = 455 list_append.append(my_var) print(list_append) |
This simple code will add number 4 at the end of the list.
[1, 2, 3, 455]
extend
The extend function doesn’t work for a non-iterable object. Try to run this code:
1 2 3 4 5 |
list_extend = [1, 2, 3] my_var = 455 list_extend.extend(my_var) print(list_extend) |
You are going to get an error.
TypeError: 'int' object is not iterable
append and extend for iterable objects
An iterable object is an object that can be accessed with an index, such as a string, or list.
1 2 |
my_string = 's' print(my_string[0]) |
This code will return the single letter ‘s’.
Let’s compare these two functions by adding a string consisting of a single character.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
list_append = [1, 2, 3] list_extend = [1, 2, 3] my_var = '4' list_append.append(my_var) list_extend.extend(my_var) print(list_append) print(list_extend) print('Append size: ' + str(len(list_append))) print('Extend size: ' + str(len(list_extend))) |
If you run the code, this is the result you are going to get:
[1, 2, 3, '4'] [1, 2, 3, '4'] Append size: 4 Extend size: 4
Both methods return the same result. But looks what happens if you change my_var to:
1 |
my_var = '455' |
The result:
1 2 3 4 |
[1, 2, 3, '455'] [1, 2, 3, '4', '5', '5'] Append size: 4 Extend size: 6 |
Now, we can clearly see the difference. The append method adds string as a single object, increasing the size from 3 to 4.
The extend methods iterates through each element and adds them as separate elements. The string ‘455’ consists of 3 elements, therefore the size increased from 3 to 6.
append and extend for lists
Other iterable objects you can use are lists. Run this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
list_append = [1, 2, 3] list_extend = [1, 2, 3] my_list = [4, 5, 5] list_append.append(my_list) list_extend.extend(my_list) print(list_append) print(list_extend) print('Append size: ' + str(len(list_append))) print('Extend size: ' + str(len(list_extend))) |
The result is similar to the one with strings. It appends a list as a single object with the append method but iterates through each element (and adds at the end of the list) with extend.
[1, 2, 3, [4, 5, 5]] [1, 2, 3, 4, 5, 5] Append size: 4 Extend size: 6
The extends method iterates through each element of the list. But if a list consists of another list it treats each list as a single element.
Change the my_list, so it looks like this:
1 |
my_list = [[4, 5], [6, 7, 8]] |
Run the code, you are going to get the following result.
1 2 3 4 |
[1, 2, 3, [[4, 5], [6, 7, 8]]] [1, 2, 3, [4, 5], [6, 7, 8]] Append size: 4 Extend size: 5 |
In this case, the extend method extended the list with another list because it iterates through the first level.
extend for a two-dimensional list
You can easily create a function that will extend a list with single elements from a 2d list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def extend2d(list, another_list): for elem in another_list: list.extend(elem) return list list_extend = [1, 2, 3] list_extend2d = [1, 2, 3] my_list = [[4, 5], [6, 7, 8]] list_extend.extend(my_list) extend2d(list_extend2d, my_list) print(list_extend) print(list_extend2d) print('Extend size: ' + str(len(list_extend))) print('Extend 2d size: ' + str(len(list_extend2d))) |
The function extend2d takes two lists as arguments. The first list is a list with values we want to extend, and the second one is a list of lists.
This function iterates through the first level of the list, so it receives a one-dimensional list that is added at the ends with the extend method.
Difference between extend and (+) operator
You can use the (+) operator in the same way as the extend function.
1 2 3 4 5 |
list_extend = [1, 2, 3] to_add = [4, 5] list_extend += to_add print(list_extend) |
If you try to print the list, you are going to get the following result:
1 |
[1, 2, 3, 4, 5] |
This is another example of when extend and (+) work in the same way.
1 2 3 4 5 6 7 8 |
list_extend = [1, 2, 3] my_tuple = (4, 5, 6) my_dictionary = {'value': 1} list_extend.extend(my_tuple) list_extend.extend(my_dictionary) print(list_extend) |
The extend function will automatically extend new elements to a list.
[1, 2, 3, 4, 5, 6, 'value']
To have the same result with the plus operator, you have to modify the code like this:
1 2 3 4 5 6 7 8 |
list_extend = [1, 2, 3] my_tuple = (4, 5, 6) my_dictionary = {'value': 1} list_extend += my_tuple list_extend += my_dictionary print(list_extend) |
The result is the same as before.