The append method is useful to add an element to the end of an existing Python List. To append multiple elements at once or insert an element at a specific index, the Python extend and Python insert methods are useful, re­spec­tive­ly.

How does Python append work?

With the append method you can add an element to the end of an existing list. Append accepts elements of any data type, making it versatile for both simple data types like integers and floating-point numbers, and more complex data types like nested lists.

The syntax for the method is:

list.append(element)
Python

Here list is the name of the list, and element is the element to be added. In the following example, elements of different data types are added to the same list:

list = [1, 2, 3]
list.append(4)
number = "Five"
list.append(number)
print(list)   # Output: [1, 2, 3, 4, 'Five']
Python

As can be seen, you can use append() to add an element of any data type to a list in Python. You can pass the element directly as a parameter or create it as a variable be­fore­hand. However, it’s worth noting that at­tempt­ing to add multiple elements at once using append() will reveal its lim­i­ta­tions. Let’s il­lus­trate this lim­i­ta­tion with an example:

list2 = [6, 7]
list.append(list2)
print(list)   # Output: [1, 2, 3, 4, 'Five', [6, 7]]
Python

In this case, when the append() method is called with the list list2 as a parameter, it adds list2 itself as a single element to the target list. This leads to a nested list, which may not be the intended outcome. To address this issue, you can resolve it by iterating over list2 with a Python for loop and adding each element in­di­vid­u­al­ly to the target list.

for number in list2:
    list.append(number)
print(list)    # Output: [1, 2, 3, 4, 'Five', [6, 7], 6, 7]
Python

The solution is a bit messy. In such cases Python extend is the better choice, which can add multiple elements to a list at once.

Ap­pli­ca­tion to other data struc­tures

While Python-append is most used for managing lists, it can be applied to other common data struc­tures in Python. However, there are a few important dif­fer­ences that you need to be aware of.

Arrays

Python Arrays aren’t part of the standard Python library and must be imported from an external Python module such as numpy or array. If ef­fi­cien­cy in terms of space and pro­cess­ing power is a priority, arrays are often a superior option compared to lists. However, arrays have certain lim­i­ta­tions in that they can only hold elements of the same data type, and this data type must be specified when creating the array. Con­se­quent­ly, using the append() method to add elements of different data types to an array is not possible. Let’s il­lus­trate this lim­i­ta­tion with an example:

import numpy as np
array1 = np.array('i', [100, 101, 102])
array1.append(103)
print(array1)
# Output: array('i', [100, 101, 102, 103])
array1.append('a')   # This line triggers an error message
Python

In addition, it’s important to note that arrays have a fixed length that is set when the data structure is created and cannot be altered af­ter­wards. For instance, if you create an array [1, 2, 3, 4, 5], it will have a length of 5. Although Python allows the usage of append() to add an extra element to an array, this operation ne­ces­si­tates the entire array to be deleted and rebuilt in the back­ground, in­cor­po­rat­ing the ad­di­tion­al element. This should be con­sid­ered when aiming to utilize system resources ef­fi­cient­ly.

Deque

As with lists and arrays, deque data struc­tures in Python behave in a similar manner. A deque, short for double-ended queue, is a data structure where elements can be added or removed from both ends. In this context, the append method behaves similarly to that of a list. However, deques offer an ad­di­tion­al method called ap­pendleft(). Unlike append(), this method inserts the element on the left side of the deque.

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

Example of how to use append()

The Python append method is a good choice when you need to perform ad­di­tion­al steps while adding elements to a list. Let’s consider an example to il­lus­trate this. Suppose we have a list of large numbers, and we want to divide each number by 7 and store the remainder in a second list:

large_numbers = [2141, 5683, 456, 789, 120, 101, 89005]
remaining_amount = []
for number in large_numbers:
    remainder.append(number % 7)
print(large_numbers)    # output: [2141, 5683, 456, 789, 120, 101, 89005]
print(residual_amount)    # output: [6, 6, 1, 5, 1, 3, 0]
Python

As can be seen, append() proves useful when dealing with elements and adding them one at a time.

Tip

Looking to deploy your website or other web ap­pli­ca­tion quickly and easily via Git? Then Deploy Now from IONOS is the perfect platform for you.

Go to Main Menu