How to Add Elements to a Python Array?

An array data structure in Python stores the collection of data having similar data types. The list is also referred to as a dynamic array in Python and can also be used as an array. Python allows us to perform various operations on an array such as adding, removing, replacing, etc. To add elements to an array various methods are used in Python, such as “insert()”, “append()”, etc.

In this blog, we will demonstrate different methods to add elements to an array in Python using multiple examples. The below contents will be briefly explained in this guide:

How to Add Elements to an Array?

The square brackets(lists) are used to initialize an array in Python. To add elements to an array different methods are used in Python, such as append(), insert(), etc. 

Let’s see different examples of how to add elements to an array:

Example 1: Using insert() Function

The below code uses the “insert()” function to add elements to an array:

Code:

array1 = [45, 50, 60, 70]
array1.insert(3, 55)
print(array1)
  • The array named “array1” is initialized in the program.
  • The “insert()” function adds the specific element by accepting the index as a first argument and the new values as a second argument.

Output:

The specific element “55” has been added to the index position “3” in the given array.

Example 2: Using “+” Operator

The below code uses the concatenation operator to add elements to an array:

Code:

array1 = ['A', 'B']
array2 = ['C', 'D']
print(array1 + array2)
  • Two arrays named “array1” and “array2” are initialized in a program.
  • The “+” operator is used to add/concatenate the elements to the given arrays.

Output:

The specific elements have been successfully added to an array.

Example 3: Using append() Function

The following code uses the “append()” function to add the elements to an array:

Code:

array1 = ['A', 'B']
array1.append('C')
print(array1)

The “array1.append()” function appends the specified element “C” to the given array.

Output:

The specific value has been added to the given array.

Example 4: Using extend() Function

The given below code is used to add elements to an array using the “extend()” function:

Code:

array1 = [1, 2, 3, 4, 5]
array2 = [15, 25]
array1.extend(array2)
print(array1)

The “extend()” function takes the specific elements of “array2” as an argument and adds it into the given array named “array1”.

Output:

The elements of array2 have been added to array1.

How to Add Items/Elements to an Array Via Array Module?

An array can also be created using the “array” module in Python, as shown in the following examples:

Example 1: Using + Operator

The following code uses the “+” operator to add the elements to an array:

Code: 

import array
array1 = array.array('i', [55, 50, 40])
array2 = array.array('i', [45, 25, 26])
output = array1 + array2
print(output)
  • In this program, the “array” module is imported.
  • The “array.array()” function is used to create the array.
  • The “+” operator concatenates the complete array elements of various arrays.

Output: 

The entire array2 has been added to array1.

Example 2: Using insert() Function

The insert() function is used in the below code to add the specific elements to an array:

Code: 

import array
array1 = array.array('i', [55, 50, 40])
array1.insert(1, 26)
print(array1)

The “array.insert()” function is used to add an element to an array named “array1”.

Output: 

The specific element has been added to the given array.

Example 3: Using append() Function

In the below code, the “append()” function is used to append/add the element at the end of an array:

Code: 

import array
array1 = array.array('i', [55, 50, 40])
array1.append(26)
print(array1)

The “array.append()” takes a specific element as an argument and appends it at the end of the given array named “array1”.

Output: 

The specified element has been added to the given array.

Example 4: Using extend() Function

The following code adds the elements to an array using the “extend()” function:

Code: 

import array
array1 = array.array('i', [3,5,7,9])
array2 = array.array('i', [1,2,4,6])
array1.extend(array2)
print(array1)

The “extend()” function accepts the “array2” as an argument and adds it to another array named “array1”.

Output: 

The element of array1 has been added to another array.

How to Add/Insert Items to a Numpy Array?

In Python, a Numpy array function named “numpy.array()” can be utilized to make an array. To add items to a numpy array various methods are used in Python. 

The following examples demonstrate how to insert items to Python NumPy array:

Example 1: Using insert() Function

The “numpy.insert()” function is used to add elements to an array in the following code:

Code: 

import numpy
array_value = numpy.array([45, 55, 60, 80])
output = numpy.insert(array_value, 3, 100)
print(output)
  • The “numpy.array()” function is used to initialize the numpy array in Python.
  • The “numpy.insert()” function accepts three parameters. 
  • The first parameter accepts an array to be modified.
  • The second parameter takes the index value and the third parameter accepts a new element that will be added to the given array.
  • The “numpy.insert()” function adds the given element to a specific index position.

Output: 

The element value has been successfully added.

Example 2: Using append() Function

The below code uses the “numpy.append()” function to add an element to an array:

Code: 

import numpy
array_value = numpy.array([45, 55, 60, 80])
output = numpy.append (array_value, [0, 100, 200])
print(output)

The “numpy.append()” function accepts the input array and a new array as arguments and adds the new array with the input array.

Output: 

Two arrays have been appended successfully.

Conclusion

To add elements to an array the “insert()” function, “+ operator”, “append()” function, and “extend()” function are used in Python. The “insert()” is the most commonly used function to add a specific element to the given array or numpy array in Python. We can also use the “append()” function to add single or multiple elements at the end of an array. This guide presented various methods to add elements to a list array, numpy array, and array of array modules.