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
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.
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
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.
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.
This simple code will add number 4 at the end of the list.
The extend function doesn’t work for a non-iterable object. Try to run this code:
You are going to get an error.
append and extend for iterable objects
An iterable object is an object that can be accessed with an index, such as string, or list.
This code will return the single letter ‘s’.
Let’s compare these two functions by adding a string consisting of a single character.
If you run the code, this is the result you are going to get:
Both methods return the same result. But looks what happen if you change my_var to:
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 add 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:
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.
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:
Run the code, you are going to get the following result.
In this case, the extend method extended the list with another list because it iterates through the first level.
extend for two-dimensional list
You can easily create a function that will extend a list with single elements from a 2d list.
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.
If you try to print the list, you are going to get the following result:
This is another example when extend and (+) work in the same way.
The extend function will automatically extend new elements to a list.
To have the same result with the plus operator, you have to modify the code like this:
The result is the same as before.