Digital Communication


Digital Communication

Python extend: merging lists


The Python extend() method allows developers toadd items from a second list to an existing list. Important element: note that the elements will be added at the end of the list. If you are looking to add an element to a specific location in a list, or just add individual elements, the Python insert or Python append methods will be better choices.

How Python extend() works and syntax

How the Python method works extend()is very simple: the method adds a list B to the end of another list Aso that the first element of list B is after the last element of list A directly. From this point on, all items in List B are added to List A. Developers call this process a concatenation. The syntax of extend()is simple as shown in the following line of code:



In this example, the elements of listeBare added at the end of listeA. But the method does not return a new list with all elements: instead, it directly modifies the original list. List B is not not modifiedunlike the initial list A. The following example concretely illustrates how the Python method works extend():

listeA = ["un", "deux", "trois"]
listeB = ["quatre", "cinq", "six"]
print(listeA) # Sortie: [‘un’, ‘deux’, ‘trois’, ‘quatre’, ‘cinq’, ‘six’]
listeA.extend([7, 8, 9])
print(listeA) # Sortie: [‘un’, ‘deux’, ‘trois’, ‘quatre’, ‘cinq’, ‘six’, 7, 8, 9]


This example shows it well: the method behaves exactly as expected. It is possible either to create the list to attach in advance as an object, or to define it as a parameter when calling the method. The example also shows that the elements of the lists to be merged can be of any data type. Indeed, a Python list can be heterogeneous: it can contain data of different types.

Although the method extend()under Python is most often used to concatenate two lists, it can also be used for other applications to know. Not only does the extend() method accept lists as parameters, but also all iterable (i.e. objects that contain elements that can be iterated over). Among these iterables, we can cite among others Python Lists, Python Strings or even Python Dictionaries. When calling the method extend(), it is possible to take any iterable as a parameter. The elements of the iterable are then added one by one to the list. The following example illustrates this property:

chars = [‘a’, ‘b’, ‘c’]
string = "def"
print(chars) # Sortie: [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]


In this example, a character string was passed as an argument. The different elements of the string (in this case the characters) were added one by one to the list.

Alternatives to the extend() method in Python

The extend() method in Python provides an elegant solution for extending a list to the elements of another list. Other alternative methods also fulfill this function.

Addition operator

The addition operator (the plus sign) is one of the most common Python operators. It is generally used to add two numbers. However, it can also be used for other purposes in Python – like merging lists. The addition operator even allows to merge any number of lists simultaneouslyas shown in the following example:

europe = ["France", "Allemagne"]
asie = ["Chine", "Inde"]
afrique = ["Soudan", "Mali"]
pays = europe + asie + afrique
print(europe) # Sortie: [‘France’, ‘Allemagne’]
print(asie) # Sortie: [‘Chine’, ‘Inde’]
print(afrique) # Sortie: [‘Soudan’, ‘Mali’]
print(pays) # Sortie: [‘France’, ‘Allemagne’, ‘Chine’, ‘Inde’, ‘Soudan’, ‘Mali’]


We see it here: this method gives almost the same result as the method extend()…with one important difference. Unlike the extend() method, the addition operator does not modify any of the lists entered as a parameter. Instead, the result is returned as a new list. As a new object is created in memory, the addition operator is a little less efficient than the extend() method when it comes to merging two lists. This is a point that can make a difference if you are working with very large lists or if you are looking to optimize your program.

Using the operator concatenation (+=) instead of addition operator, no additional list will be created but the original list will be modified. This alternative makes it possible to circumvent the disadvantage of the addition operator, even if extend()And +=do not work the same for all iteratives.

The append() method

The append method works like extend()– with this difference thatit can only add one element to a list. With this method, it is impossible to pass an iterable. Here is an example to illustrate this:

liste = [0, 1, 1, 2]
print(liste) # Sortie: [0, 1, 1, 2, 3]
print(liste) # Sortie: [0, 1, 1, 2, 3, 5]


In this example, extend()And append()output the same result, except that the method append()does not require indicating the element to be added in square brackets. But if we want to add several elements to a list with append(), it would be necessary to go through an iteration on an already existing list with a Loop For Python. This is why we generally do not use append()only when we want to add a single element.

Looking to get your website or web application online quickly, easily and live? So use Git with Deploy Now by IONOS, a platform designed for you!

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact