Python Program to Append an Element Into an Array


An array is a collection of elements of same data type, and each element in the array is identified by an index value. It is a simplest data structure and we can easily add or remove elements.

Arrays in Python

Python does not have a specific data structure to represent arrays. Here, we can use List an array.

 [9, 3, 1, 6, 9]

We can use array or NumPy module to work with arrays in python.

array('i', [1, 2, 3, 4])

The above array is the integer Array which is defined by the array module.

In the same way we can also define a Numpy array using the NumPy module.

array([1, 2, 3, 4])

The indexing in python starts from 0. The above all arrays elements are also indexed from 0, 1,.., (n-1).

Input Output Scenarios

Assuming we have an input array with integer values. And the resultant array will have an element appended to it.

Input array:
A = [1, 5, 3, 6]
Output array:
[1, 5, 3, 6, 2]

The integer element 2 is appended at the end of the given array.

In the below article, we see multiple ways to append an element into an array in Python.

Using List Data Structure

As we are using List as an array, we can use the list.append() method to append elements to the array.

Syntax

list.append(element) 

It adds an element to the end of the list. Which is equivalent to a[len(a):] = [x].

Example

lst = [1, 2, 3, 4, 5, 6] 
print ("The original array is: ",lst) 
print() 

# append an element 
lst.append(9)
print ("The resultant array is: ",lst) 

Output

The original array is:  [1, 2, 3, 4, 5, 6]

The resultant array is:  [1, 2, 3, 4, 5, 6, 9]

The element 9 is appended to the array, and which is added at the end of the array.

Using the Array Module

The array module in Python allows us to create an array, and that can compactly represent an array. To use the array module initially we need to import the array module.

Syntax

array.append(x) 

Appends a new item with value x to the end of the array.

Example

import array
 
# creating array
int_array = array.array('i', [1, 2, 3, 4])
print ("The original array is: ",int_array) 
print() 

# append an element 
int_array.append(0)
print ("The resultant array is: ",int_array) 

Output

The original array is:  array('i', [1, 2, 3, 4])

The resultant array is:  array('i', [1, 2, 3, 4, 0])

The integer type is specified to the int_array object at the time of creation. And if we try to append any other type element to the array object, then it will raise an Error like below.

TypeError − integer argument expected, got float

Using the NumPy Module

By using the numpy library we can easily create an array using the numpy.array() method. In the same way, we can also append an element to the array using the numpy.append() method.

Syntax

numpy.append(array, element) 

The method appends an element to the end of an array. And it creates a new array which can be the copy of old array with the appended element so that the original array remains unchanged.

Example

In this example, we will iterate the string array elements using for loop.

import numpy
 
# creating array
array = numpy.array([1, 2, 3, 4])
print ("The original array is: ", array) 
print() 

# append an element 
result = numpy.append(array, 9)
print ("The resultant array is: ", result) 

Output

The original array is:  [1 2 3 4]

The resultant array is:  [1 2 3 4 9]

Here the original array remains the same and the resultant array got updated with the new element.

Updated on: 15-May-2023

958 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements