Skip to content
Home » How To Append an Array in Python

How To Append an Array in Python

In this tutorial let’s learn about How To Append an Array in Python.

An array is a linear data structure that stores similar types of components in programming language. Python does not have an array data type. You can instead use a Python list or a numpy array instead.

In various computer languages, a list is similar to an array. If you consider the list to be an array, then use the list append() method to append an item to the array.

Types of Python Arrays

An array is a linear data structure that stores similar types of components in programming language. Python provides us the below Types of arrays :

  • Python List: It has all of the capabilities of an Array.
  • Array module in Python : This module is used to generate an array and manipulate its data using the functions given.
  • NumPy array: The NumPy module generates an array for mathematical applications.

Python array append

The append() method in Python allows us to attach an element or array to the end of another array. To put it another way, the supplied element is appended to the end of the input array.

Syntax:

list.append(array or element )

You can append a single element or an array to the existing array ( lists).

Example:

list = [3,6,1,8]
print('Initial List :',list)


list.append(9)
print("List After appending a new element :",list)

Output :

Initial List : [3, 6, 1, 8]
List After appending a new element : [3, 6, 1, 8, 9]

Append an Array to Python Lists

You can append or add an array to the lists using append() method. The array will be added at the end of the current array/list. Below are the syntax and code to append an array to python lists.

Syntax:

list.append(element or array)

The element or array is added to the end of the list, and the list is updated with the new element.

Code Example :

list = [1,2,3]
print('Initial List :',list)

array = [4,5,6]
list.append(array)

print("List After appending an array :",list)

Output :

Initial List : [1, 2, 3]
List After appending an array : [1, 2, 3, [4, 5, 6]]

Array module in Python

Using the Array module, we can create an array and then append elements to it using the append() function. Import the array module to begin working with it.

In Python, use the array.array() function to initialize an array created with the array module.

import array
array.array('unicode',elements)

In the above unicode is defined as : It represents the type of elements that will be occupied by the array. For example, the letter ‘d’ stands for double/float elements.


Furthermore, the append() function works in the same way as Python Lists do.

Code Example :

import array 
x = array.array('i', [1,2,3])
x.append(4)

print("After appending an array :",x)

Output :

After appending an array : array(‘i’, [1, 2, 3, 4])

Python Numpy Array append

The NumPy module could be used to generate an array and then alter the data using various mathematical functions.

Syntax:

numpy.append(array, value, axis)

array: A numpy array to which the data to be added.
value: The array data to be added.
axis(Optional): It defines row- and column-wise operations.

Code Example :

import numpy as np 
 
a = np.arange(4) 
print("Array a : ", a) 
 
b = np.arange(5,10) 
print("Array b : ", b) 
 
c = np.append(a, b)
print("Result after appending a and b: ", c) 

Output :

Array a : [0 1 2 3]
Array b : [5 6 7 8 9]
Result after appending a and b: [0 1 2 3 5 6 7 8 9]

Conclusion

In this tutorial we learnt different ways of appending an array in python. Mainly covered the python lists, array module and the Numpy arrays.

Similar Posts:

‘b’ Character in Front of String in Python
Set Comprehension in Python
Create Empty List in Python