How to append an Array in Python

In this article, we’ll look at different approaches to append an array in Python.

If we think about it in programming terms, an array is a linear data structure that stores elements of the same type as the elements in the array.

It is well known that the Python programming language does not provide us with a specific data type “array.” The following Python Array variants are available for us to utilize instead.

  • List: This type of object contains all of the functionality of an Array.
  • Array Module: The Python Array module is used to create arrays and manipulate the data within them using the functions that are specified.
  • NumPy array in Python: The NumPy module creates an array that can be used for mathematical operations.

Let’s take a look at the various methods of appending elements to the Python Arrays described previously.

Using append() Function To Append An Array In Python

The append() function in Python allows us to append an element or an array to the end of another array, which is useful when working with large arrays. In other words, the specified element is appended to the end of the input array when it is specified.

The append() function has a different structure depending on which variant of the Python array you are working with. Let’s take a look at how the Python append() method works on each of the different variants of the Python Array.

Using append() with List

Lists are regarded as dynamic arrays in computer science. The append() method in Python can be used to add/append elements to the end of a list in this context.



    listName = is the variable name that holds the list.
    elementsToAdd = The value you want to append to the defined list.

The list or the element is appended to the end of the list, and the list is updated to reflect the addition of the element to the end of the list.

#Initializing A List
newList = [1,2,3,4,5]

#Appending A Single Element
#To Above List

#Initializing A List To Append
listToAppend = [7,8,9]

#Appending the List to newList

#Printing the newList with Updated Values


[1, 2, 3, 4, 5, 6, [7, 8, 9]]

Using Array Module To Append

We can use the Array module to create an array, and then we can use the append() function to add elements to the array we just created.


import array

newArray = array.array('unicode',elements)

     newArray = It will hold the array values
     unicode  = It represents the type of elements to be occupied by the array. For example, ‘d’ represents double/float elements.

Furthermore, the append() function behaves in the same way as the append() function when dealing with Python Lists.

#importing Array Library
import array

#Initializing new Array with Integers
newArray = array.array('i', [10,11,12,13])

#appending Single Element

#Printing The new Array


array('i', [10, 11, 12, 13, 14])

Using NumPy Array To Append

With the help of the NumPy module, it is possible to create an array and manipulate the data using a variety of mathematical functions.



    arrayToAppend = It is the Array to which you want to add the value.
    valueToAppend = It is the value that you want to append to array.
    axis = This is optional parameter, It specifies row-wise or column-wise operations.

Using the numpy.arange() method, we can create an array containing values that fall within a specified range of values as shown in the following example.

#importing NumPy
import numpy as np

#Initializing new Array with Integers
newArray = np.arange(1,6)

#appending Single Element
resArray = np.append(newArray, [6])

#Printing The res Array After Adding 6

#Initializing Array To Append
appendArray = np.arange(7,10)

#Appending Above List to result Array
resArray = np.append(resArray, appendArray)

#Printing The new Array


[1 2 3 4 5 6]
[1 2 3 4 5 6 7 8 9]
How to append an Array in Python

Wrap Up

I hope you have a good understanding of how to append an array in Python. I’ve gone over three different ways to initialize an array and how to use the append function on each of them.

This concludes the discussion on this subject. Please feel free to leave a comment if you have any questions or concerns.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Initialize A Python Array In 3 Ways
  2. Reverse an Array in Python
  3. How to Print an Array in Python
  4. Remove a Character From String In Python

Leave a Comment