Python

Numpy Array Append

NumPy is a Python library/module used in Python programming to perform scientific calculations. This lesson will teach you how to execute various operations on NumPy arrays. NumPy has a multidimensional array object, as well as derived arrays like masked arrays and masked multidimensional arrays. The numpy append() method in Python is used to join two arrays together. This function creates a new array while leaving the original array alone.

The syntax is numpy.append(arr, values, axis=None). The ‘arr’ parameter, in this case, can be an array-like object or perhaps a numpy array. A copy of this array is added with the values. The values are array-like objects added to the “arr” components at the very end. The axis determines which axis values are attached to. Both arrays are flattened if the axis is not specified.

We may utilize the “ndarray” object provided by the NumPy module to execute operations on an array of any dimension. The ndarray is an N-dimensional array in which N can be any number. As a result, NumPy arrays can be of any size. In comparison to Python lists, NumPy has a lot of advantages. NumPy arrays can be used to conduct high-performance operations like sorting array members, mathematical and logical operations, input/output functions, and statistical and linear algebra calculations. In this post, we’ll look at how to use the append(), concatenate(), and insert() functions to add or append a single element to a numpy array. Let’s get started.

Example 1

In this example, we’ll use append to add an element to the numpy array. The Numpy module in Python has a function called numpy.append() that allows you to add an element to a numpy array. The add() function can take a numpy array and a single value as parameters. It returns a copy of the passed array with the provided value attached rather than modifying the existing array. Consider the code below as an example. After importing numpy, we created an integer numpy array. The numpy.append() function is then utilized to attach an element to the very end of a numpy array. Finally, we printed both the original and updated arrays.

import numpy as np
myarr = np.array([22, 3, 4, 7, 1])
n_arr = np.append(myarr, 5)
print('Newly Created Array is: ', n_arr)
print('Original Array is: ', myarr)

You can see the new and original array in the attached screenshot. The append() function copied the array, then appended the number 5 to its end before returning it.

Example 2

We’ll use the concatenate method to add the element to the NumPy Array. Numpy.concatenate() is a method in the Python NumPy module that joins two or more arrays. That allows us to add a single element to a NumPy array. However, we must encapsulate the single item in a sequence data structure, such as a list, and feed the concatenate() function a tuple of array and list. For instance, take a look at this code.

As you can see in the third line of code, you can append an element to the end of a NumPy array. It created a new array with entries from the array plus list sequences. It didn’t change the original array but instead returned a new array with all of the original NumPy array’s contents plus a single value appended at the end.

import numpy as np
myarr = np.array([22, 3, 4, 7, 1])
n_arr = np.concatenate( (myarr, [5] ) )
print('Newly Created Array is: ', n_arr)
print('Original Array is: ', myarr)

The attached screenshot shows the original and newly created arrays.

Example 3

The insert() method in NumPy can also insert an element or column. The difference between the insert() and append() methods is that the insert() function allows us to specify the index at which we wish to add an element, whereas the append() method adds a value to the end of the array. Consider the following scenario. Here you can see that the insert() function was called with three arguments: a NumPy array, an index point, and a value to be added. It generated a copy of myarr with the value of the specified index position added. We selected the array’s size as the index position since we expected to add the element at the end of the array. As a result, the value was appended to the end of the array. It’s important to note that it didn’t change the original array; instead, it returned a copy of myarr with the supplied value appended at the specified index, i.e., at the end of the array.

import numpy as np
myarr = np.array([22, 3, 4, 7, 1])
n_arr = np.insert(myarr, 1, 90)
print('Newly Created Array is: ', n_arr)
print('Original Array is: ', myarr)

Here you can see the newly created and the original array.

Conclusion

A NumPy array is a tuple of non-negative integers and indexes a grid of elements of all the same types. The array’s ranking is the number of dimensions; the shape is a tuple of numbers representing the array’s size and dimension. In this post, we covered three distinct methods for appending a single element to the end of a NumPy array. Working with NumPy arrays is straightforward, as we’ve shown. When working with most machine learning frameworks, NumPy arrays are important. As a result, NumPy might be considered the gateway to artificial intelligence.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content