Insert an element at the beginning of NumPy Array

In this article, we will learn how to add an element to the front of NumPy Array in Python.

Given a NumPy array, we need to add an element to the front of NumPy Array i.e. insert an element at the 0th index position of the array.

Example:             

Given array:
[1, 3, 5, 8, 9]

After adding 10 to the front of NumPy Array:
[10, 1, 3, 5, 8, 9]

There are multiple ways to add an element to the front of NumPy Array. Let’s discuss all the methods one by one with a proper approach and a working code example

1.) Using insert() to add an element to the front of NumPy Array

Numpy module in python, provides a function numpy.insert() to insert values along the given axis before the given index. The insert() method will take array, index and value to be inserted as parameters. It will insert the given value just before the specified index in a copy of array and returns the modified array copy.

Now, to add a element at the front of numpy array we need to pass the array, index as 0 and value to the insert() method i.e. insert(arr,0 , 6).

Syntax of insert()

numpy.insert(arr, obj, values, axis=None)

Parameters:
	arr          = The array to be passed to the function.
	obj          = index at which value needs to be inserted
	values       = Values to insert into array.
	axis         = int, optional, Axis along which to insert values.

Returns:
	Returns array with value inserted at the specified index, in this case, appended at the front of the array.
    

Approach

  • Import numpy library and create numpy array
  • Now pass the array, value to be inserted and set index = 0 to the insert() method
  • The insert() method will return a copy of the array by adding the element at the front of the array
  • Print the array.

Source code

import numpy as np

# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The Original array is = ", arr)

#adding an element at front of array using insert() method
arr = np.insert(arr, 0, 6)

# printing the new array
print(" The new array is = ", arr)

OUTPUT:

 The Original array is =  [1 2 3 4 5]
 The new array is =  [6 1 2 3 4 5]

2.) Using append() to add an element to the front of NumPy Array

Numpy module in python, provides a function numpy.append() to append values to the end of an array. The append method will take array, value to be appended as parameters. It will append the given value at the End of the array and returns the array.

Syntax of append()

numpy.append(arr, values, axis=None)

Parameters:
	arr          = The array to be passed to the function.
	values       = Values to be appended to array.
	axis         = int, optional, Axis along which to append values.

Returns:
	Returns array with values appended at the end.

Now in order to append the element at front of the array, we need to pass the element to be appended first and then pass the array to the append() method.

Approach

  • Import numpy library and create numpy array
  • Now pass the value to be appended and array to the append() method.
  • The append method will return a copy of the array by adding the element at the front of the array
  • Print the array

Source code

import numpy as np

# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The Original array is = ", arr)

#appending an element at front of the array using append() method
arr = np.append(6, arr)

# printing the new array
print(" The new array is = ", arr)

OUTPUT:

 The Original array is =  [1 2 3 4 5]
 The new array is =  [6 1 2 3 4 5]

3.) Using concatenate() to add an element to the front of NumPy Array

Numpy module in python, provides a function numpy.concatenate() to join a sequence of arrays along an existing axis. The concatenate() method will take a sequence of arrays as parameters. It will concatenate the arrays into one single array and returns the array.

Syntax of concatenate()

numpy.concatenate((a1, a2, ...), axis=0)

Parameters:
	(a1, a2, ...) = sequence of arrays to be passed to the function.
	axis          = int, optional, Axis along which to concatenate arrays.

Returns:
	Returns a concatenated array.

Now to append an element at front of an array. We need to pass the element to be appended first and then pass the array to the concatenate() method. It will return a copy of the array with the element added at the front.

Example

concatenate(([6] , [1,2,3,4,5] )) ==> [6, 1, 2, 3, 4, 5]

Approach

  • Import numpy library and create numpy array.
  • Now pass the value to be appended and array as sequence of array to the concatenate method.
  • The concatenate() method will return a copy of array by adding the element at the front of array.
  • Print the array.

Source code

import numpy as np

# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The Original array is = ", arr)

# Adding an element at front of array using concatenate() method
arr = np.concatenate(([6], arr))

# printing the new array
print(" The new array is = ", arr)

OUTPUT:

 The Original array is =  [1 2 3 4 5]
 The new array is =  [6 1 2 3 4 5]

4.) Using hstack() to add an element to the front of NumPy Array

Numpy module in python, provides a function numpy.hstack() function is used to stack the sequence of input arrays horizontally i.e, concatenating into a single array. The hstack() method will take sequence of arrays as parameters. It will concatenate the arrays into one single array and returns the array. The hstack() is equivalent to concatenation.

Syntax of hstack()

numpy.hstack(tuple)

Parameters:
	tuple = sequence of arrays to be passed to the function.

Returns:
	Returns The array formed by stacking the given arrays.
    

Now to append an element at front of an array, In the sequence of array, we need to pass the element to be appended first and then the array to the hstack() method. It will return a copy of the array with the element added at the front.

Example

hstack( ([6], [1,2,3,4,5]) ) ==> [6,1,2,3,4,5]

Approach

  • Import numpy library and create numpy array
  • Now pass the value to be appended and array as sequence of array to the hstack() method
  • The hstack() method will return a copy of array by adding the element at the front of array
  • Print the array

Source code

import numpy as np

# Create a NumPy Array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The Original array is = ", arr)

# Adding an element at front of array
arr = np.hstack(([6], arr))

# printing the new array
print(" The new array is = ", arr)

OUTPUT:

 The Original array is =  [1 2 3 4 5]
 The new array is =  [6 1 2 3 4 5]

Summary

Great! you made it, We have discussed all possible methods to add an element to the front of NumPy Array.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top