AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Python append: adding list element

PARTAGEZ

We use the append() method in Python to add an element to the end of an existing Python List. If you are instead looking to add multiple elements at the same time or insert an element at a particular index, it is better to use the appropriate Python extend and Python insert methods.

append(): working under Python

The append() method allows you to add an element at the end of an existing list. The element to add can take the form of any type of data. So you can populate Python lists with simple data types (e.g. integers, floating point numbers, etc.) or more complex ones (like other lists).

The syntax of the method is as follows:

liste.append(element)

Python

In this example, listeis the name of the list and elementrepresents the element to add. In the following example, we add elements of different types to the same list:

liste = [1, 2, 3]
liste.append(4)
nombre = "cinq"
liste.append(nombre)
print(liste)   # Sortie : [1, 2, 3, 4, ‘cinq’]

Python

As we can see, append()allows you to add an element of any type of data to a list in Python. It is possible to directly enter the element to add as a parameter, or to create it beforehand as a variable before adding it to the list. However, the method append()shows its limitations if you try to add multiple elements at the same time, as shown in the following example:

liste2 = [6, 7]
liste.append(liste2)
print(liste)   # Sortie : [1, 2, 3, 4, ‘cinq’, [6, 7]]

Python

In this case, the list liste2was passed as a parameter when calling the method. Instead of adding the elements of liste2to the other list, the method simply added liste2itself as a single element. The result is a nested list, which is not the intended purpose. We can solve this problem by iterating over liste2with a for loop in Python and adding each element separately.

for nombre in liste2:
   liste.append(nombre)
print(liste)   # Sortie : [1, 2, 3, 4, ‘cinq’, [6, 7], 6, 7]

Python

This solution is not the most aesthetic. That’s why, in such cases, the Python extend method remains the best choice by adding multiple elements at once to a list.

Application to other data structures

Although the Python append() method is most often used for list management, it can also be applied to other common data structures under Python. However, there are some important differences to take into account.

Arrays

Python Arrays are not part of the standard Python library and must be imported from an external module like numpyOr array. But, if you are looking to effectively manage memory space and computing power, these tables are a better choice than lists. On the other hand, arrays are a little more limited: they can only contain elements of data of the same type. The data type is defined when the table is created. It is therefore impossible to add elements of different data types to an array with the append() method. The following example illustrates why:

import numpy as np
array1 = np.array(‘i’, [100, 101, 102])
array1.append(103)
print(array1)

array1.append(‘a’)   # Cette ligne affiche un message d’erreur

Python

Finally, it is important to mention that arrays have a fixed length, determined when the data structure is created. It is impossible to modify this structure afterwards. If you create the table [1, 2, 3, 4, 5]it will have a length of 5. Although Python certainly allows you to add an additional element to an array with append()this goes through removing the entire array in the background and rebuilding it with the extra element. It is good to take this into account to make the best use of system resources.

Deque

As with lists and arrays, there is an append() method for Deque data structures with very similar behavior. A “Deque” (pronounced “Dèque”, abbreviation of the English double-ended tail) is a list in which we can add or remove elements both at the end and at the beginning. Here, the append() method behaves the same way as for a list. There is, however, an additional method, appendleft(). Unlike add(), this method inserts the element to the left of the Deque.

from collections import deque
entiers_naturels = deque([1, 2, 3])
entiers_naturels.append(4)
entiers_naturels.appendleft(0)
print(entiers_naturels)   # Sortie : deque([0, 1, 2, 3, 4])

Python

Practical case: how to use append()

True, the Python append() method only allows individual elements to be added to the end of a list. However, it is ideal if you want to go further in adding an element. The following example illustrates the particularity of the method: in this case, we have a list of large numbers. Each number is divided by 7 and the remainder of this is added to a second list.

grands_nombres = [2141, 5683, 456, 789, 120, 101, 89005]
reste = []
for nombre in grands_nombres:
   reste.append(nombre % 7)
print(grands_nombres)   # Sortie : [2141, 5683, 456, 789, 120, 101, 89005]
print(reste)   # Sortie : [6, 6, 1, 5, 1, 3, 0]

Python

As this example shows, append() is very useful when it comes to manipulate individual elements when adding them.

Do you want to deploy your website or other web application? The quick, easy and direct solution is to use Git with Deploy Now by IONOS – a platform made 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