How to Append to an Array in Python?

As a data scientist working to design an algorithm in the USA, I often need to append to an array in Python. I explored many ways to achieve this, In this tutorial, I will give detailed examples and explanations of various methods for easy understanding.

Introduction to Python Arrays

Before getting into the methods of appending elements, it’s essential to understand what Python arrays are and how they function. In Python, arrays can be created using the array module or by using lists.

Check out How to Find the Maximum Value in an Array in Python

1. Using the array Module

The array module provides an efficient way of storing basic data types. Here’s how you can create an array using this module:

import array

# Create an array of integers
arr = array.array('i', [10, 20, 30])
print(arr)

2. Using Python Lists

Lists are more flexible and are used more frequently in Python programming. Here’s an example of creating a list:

# Create a list of integers
arr = [10, 20, 30]
print(arr)

Read How to Create an Array of Zeros in Python

Methods to Append to a Python Array

There are several methods to append elements to a Python array. We’ll cover the most common methods.

Method 1. Using the append() Method

The append() method is used to add a single element at the end of an array. This method modifies the original array.

Example: Appending a Single Element

Let’s append a single element to an array of city populations in the USA:

# Create a list of city populations
city_populations = [8398748, 3990456, 2705994]  # New York, Los Angeles, Chicago

# Append a new city's population
city_populations.append(2325502)      #Houston
print(city_populations)         

Output:

[8398748, 3990456, 2705994, 2325502]

I executed the above Python code using VS code, you can see the exact output in the below screenshot.

Append to an Array in Python

In this example, we started with the populations of New York, Los Angeles, and Chicago, and then appended Houston’s population to the list.

Check out How to Print an Array with Commas in Python

Method 2. Using the extend() Method

The extend() method is used to add multiple elements to the end of an array. This method also modifies the original array.

Example: Appending Multiple Elements

# Create a list of city populations
city_populations = [8398748, 3990456, 2705994]  # New York, Los Angeles, Chicago

# Extend the list with more city populations
city_populations.extend([2325502, 1660272])  
print(city_populations)

Output:

[8398748, 3990456, 2705994, 2325502, 1660272]

I executed the above Python code using VS code, you can see the exact output in the below screenshot.

How to Append to an Array in Python

In this example, we extended the list by adding the populations of Houston and Phoenix.

Read How to Find the Sum of an Array in Python

Method 3. Using List Comprehensions

List comprehensions provides a simple way to create lists. They can also be used to append elements to an array by creating a new list that includes the original elements plus the new ones.

Example: Using List Comprehensions

# Create a list of city populations
city_populations = [8398748, 3990456, 2705994]  # New York, Los Angeles, Chicago

# Append a new city's population using list comprehension
city_populations = [*city_populations, 2325502]  
print(city_populations)

Output:

[8398748, 3990456, 2705994, 2325502]

I executed the above Python code using VS code, you can see the exact output in the below screenshot.

Append to an Array in Python Using List Comprehensions

In this example, we used a list comprehension to create a new list that includes the original elements plus Houston’s population.

Check out How to Remove Duplicates from an Array in Python

Handling Python Arrays with NumPy

For numerical data and more complex operations, the NumPy library is preferred due to its efficient features.

Installing NumPy

If you haven’t already installed NumPy, you can do so using pip:

pip install numpy

Creating and Appending to a NumPy Array

Here’s how you can create and append elements to a NumPy array:

import numpy as np

# Create a NumPy array of city populations
city_populations = np.array([8398748, 3990456, 2705994])  # New York, Los Angeles, Chicago

# Append a new city's population
city_populations = np.append(city_populations, 2325502)  # Houston
print(city_populations)

In this example, we used NumPy’s append() function to add Houston’s population to the array.

Read How to Check if an Array Index Exists in Python

Appending Multiple Elements with NumPy

You can also append multiple elements to a NumPy array:

import numpy as np

# Create a NumPy array of city populations
city_populations = np.array([8398748, 3990456, 2705994])  # New York, Los Angeles, Chicago

# Append multiple city populations
city_populations = np.append(city_populations, [2325502, 1660272])  # Houston, Phoenix
print(city_populations)

In this example, we appended the populations of Houston and Phoenix to the array.

Check out How to Remove NaN from Array in Python

Common Issues to Avoid

When appending elements to an array, there are some common issues to watch out.

Modifying the Original Array

Both the append() and extend() methods modify the original array in place. If you need to keep the original array unchanged, make sure to create a copy before appending elements.

Appending Non-Compatible Types

Ensure that the elements you’re appending are compatible with the existing elements in the array. For example, appending a string to an array of integers will result in a TypeError.

Read How to Check if an Array Contains a Value in Python

Conclusion

In this tutorial, I explained how to append to an array in Python, by various methods, like append() method which adds a single element at the end of an array, by extend() method we can add multiple elements to the end of an array, and by using list comprehensions creating a new list that includes the original elements, I also showed how to create using the array module and by using lists.I also explained how to handle Python Arrays with NumPy, and appending multiple elements with NumPy, we also discussed common issues to avoid.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.