How to use Python append to extend lists
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, respectively.
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)
PythonHere 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']
PythonAs 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 beforehand. However, it’s worth noting that attempting to add multiple elements at once using append()
will reveal its limitations. Let’s illustrate this limitation with an example:
list2 = [6, 7]
list.append(list2)
print(list) # Output: [1, 2, 3, 4, 'Five', [6, 7]]
PythonIn 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 individually to the target list.
for number in list2:
list.append(number)
print(list) # Output: [1, 2, 3, 4, 'Five', [6, 7], 6, 7]
PythonThe solution is a bit messy. In such cases Python extend is the better choice, which can add multiple elements to a list at once.
Application to other data structures
While Python-append is most used for managing lists, it can be applied to other common data structures in Python. However, there are a few important differences 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 efficiency in terms of space and processing power is a priority, arrays are often a superior option compared to lists. However, arrays have certain limitations in that they can only hold elements of the same data type, and this data type must be specified when creating the array. Consequently, using the append() method to add elements of different data types to an array is not possible. Let’s illustrate this limitation with an example:
import numpy as np
array1 = np.array('i', [100, 101, 102])
array1.append(103)
print(array1)
array1.append('a') # This line triggers an error message
PythonIn 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 afterwards. 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 necessitates the entire array to be deleted and rebuilt in the background, incorporating the additional element. This should be considered when aiming to utilise system resources efficiently.
Deque
As with lists and arrays, deque data structures 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 additional method called appendleft(). 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]
PythonExample of how to use append()
The Python append method is a good choice when you need to perform additional steps while adding elements to a list. Let’s consider an example to illustrate 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]
PythonAs can be seen, append() proves useful when dealing with elements and adding them one at a time.
Looking to deploy your website or other web application quickly and easily via Git? Then Deploy Now from IONOS is the perfect platform for you.