How to append an Array in Python?

Python Append() Method

Hey, folks! In this article, we will focus on ways to append an array in Python.


What is Python Array?

In programming terms, an array is a linear data structure that stores similar kinds of elements.

As we all know, Python does not offer us with a specific data type — ‘array’. Rather, the following variants of Python Array are available for us to use–

  • Python List: It contains all the functionalities of an Array.
  • Python Array module: This module is used to create an array and manipulate the data with the specified functions.
  • Python NumPy array: The NumPy module creates an array and is used for mathematical purposes.

Now, let us understand the ways to append elements to the above variants of Python Array.


Append an Array in Python Using the append() function

Python append() function enables us to add an element or an array to the end of another array. That is, the specified element gets appended to the end of the input array.

The append() function has a different structure according to the variants of Python array mentioned above.

Let us now understand the functioning of Python append() method on each variant of Python Array.


Variant 1: Python append() function with Lists

Lists are considered as dynamic arrays. Python append() method can be framed here to add/append elements to the end of the list.

Syntax:

list.append(element or list)

The list or the element gets added to the end of the list and the list is updated with the added element.

Example:

lst = [10,20,30,40] 
x = [0,1,2] 
lst.append(x) 
print(lst) 

Output:

[10, 20, 30, 40, [0, 1, 2]]

Variant 2: Python append() method with the Array module

We can create an array using the Array module and then apply the append() function to add elements to it.

Initialize a Python array using the array module:

import array
array.array('unicode',elements)
  • unicode: It represents the type of elements to be occupied by the array. For example, ‘d’ represents double/float elements.

Further, the append() function operates in the same manner as that with Python Lists.

Example:

import array 
x = array.array('d', [1.4, 3.4])
y = 10
x.append(y)
print(x)

Output:

array('d', [1.4, 3.4, 10.0])

Variant 3: Python append() method with NumPy array

The NumPy module can be used to create an array and manipulate the data against various mathematical functions.

Syntax: Python numpy.append() function

numpy.append(array,value,axis)
  • array: It is the numpy array to which the data is to be appended.
  • value: The data to be added to the array.
  • axis(Optional): It specifies row-wise or column-wise operations.

In the below example, we have used numpy.arange() method to create an array within the specified range of values.

Example:

import numpy as np 

x = np.arange(3) 
print("Array x : ", x) 

y = np.arange(10,15) 
print("\nArray y : ", y) 

res = np.append(x, y)
print("\nResult after appending x and y: ", res) 

Output:

Array x :  [0 1 2]

Array y :  [10 11 12 13 14]

Result after appending x and y:  [ 0  1  2 10 11 12 13 14]

Conclusion

That’s all for this topic. Feel free to comment below, in case you come across any doubt. For more such posts related to Python, do visit Python@AskPython.


References

  • Python add to array — JournalDev
  • NumPy append() method — JournalDev