In this article, we will discuss different ways to remove first element from a NumPy Array.

Given a NumPy array, we need to remove first element from the NumPy Array i.e. delete the element at the index position 0 from the

the array.

Example: Given array = [ 1, 3, 5, 8, 9 ] After removing first element = [ 3, 5, 8, 9 ]

There are multiple ways to remove first element from a NumPy Array. Lets discuss all the methods one by one with proper approach and a working code example

## 1.) Using Slicing

Slicing in Python can be defined as selecting elements from one given index position till another given index position.

Example: arr = [ 1, 3, 5, 8, 9 ] arr[0 : 2] ===> this will give [ 1, 3, 5 ]

The index of first element is zero (0). Now to remove the first element, select the elements from index position 1 to N, where N is the size of array.

**Advertisements**

#### Approach

1. Import numpy library and create numpy array

2. Now use slicing to remove the first element by selecting elements from 1 to N (size of array is N)

3. It will remove the first element from the NumPy array.

#### Source code

import numpy as np # Creating numpy array arr = np.array([1,2,3,4,5,6,7]) # Removing the FIRST element using slicing arr = arr[1:] print(arr)

#### OUTPUT:

[2 3 4 5 6 7]

## 2.) Using delete() method

The delete() method is a built-in method in numpy library. The delete() method is used to delete the elements from the given array, the delete method takes array and a index or array of indices as parameters. It returns the array by deleting the elements at given indexes.

Now to delete the first element in the array we need to pass the given array and the index of the first element to the delete method.

The index of the first element is 0.

#### Syntax of delete()

numpy.delete(arr, obj)

#### Parameters:

arr = The array to be passed to the function. obj = index (or array of index) of the elements to be deleted.

#### Returns:

Returns array with the elements removed.

#### Approach

1. Import numpy library and create numpy array

2. Index of the first element is given as 0

3. Now pass the given array and the index of first element to the delete() method.

4. Print the array.

#### Source code

import numpy as np # Creating numpy array arr = np.array([1,2,3,4,5,6,7]) # Removing the FIRST element using delete method #INDEX of FIRST element is 0. arr = np.delete(arr, 0) print(arr)

#### OUTPUT:

[2 3 4 5 6 7]

## 3.) Using Boolean array

The elements in a numpy array can be accessed by passing a Boolean array as index to the array

Example: arr = [ 1, 3, 5, 8, 9 ] boolArray = [True, True, False, False, False] arr[boolArray] ===> this will give [ 1, 3 ]

Now to remove the first element from the array create a Boolean array with length same as the array. In at all the elements are True except for the first element. Then pass this Boolean array as index to the given array. This will give an array with first element removed.

#### Approach

1. Import numpy library and create numpy array

2. Create a Boolean array with length same as the array and All the elements as True except for the first element

3. Now Pass this Boolean array as index to the Given array

4. This will give an array with first element removed.

#### Source code

import numpy as np # Creating numpy array arr = np.array([1,2,3,4,5,6,7]) bIndex = [False, True, True, True, True, True, True] # Removing the FIRST element using boolean index arr = arr[bIndex] print(arr)

#### OUTPUT:

[2 3 4 5 6 7]

## 4.) Using index array

The elements in a numpy array can be accesed by passing a index array as index to the array

Example: arr = [ 1, 3, 5, 8, 9 ] indexArray = [1,3] arr[indexArray] ===> this will give [ 3, 8 ]

Now to remove the first element from the array create a index array with elements as indexes of all the elements except for the first element. Then pass this index array as index to the given array, This will give an array with first element removed.

#### Approach

1. Import numpy library and create numpy array

2. Create a index array with elements as indexes of all the elements except for the first element

3. Now Pass this index array as index to the given array

4. This will give an array with first element removed.

### Source code

import numpy as np # Creating numpy array arr = np.array([1,2,3,4,5,6,7]) # Removing the FIRST element using index array indexArray = [i for i in range(1,len(arr))] arr = arr[indexArray] print(arr)

#### OUTPUT:

[2 3 4 5 6 7]

## 5.) Using resize() method

The resize() method is a built-in method in numpy library. The resize() method is used to resize the given array into specified shape, the resize method takes array and a shape as parameters and returns a array by resizing the array into specified shape.

Now to delete the first element in the array, we need to rotate the array to left by one index. By doing this, the element at 0th index will go to the last index in the array. Now reshape the rotated array to length of the given array decremented by one. This will remove the last element which is basically the first element in original array. To Better understand this method, see the following example.

#### EXAMPLE

Given array = [1 2 3 4 5 6 7] Array after rotating to left = [2 3 4 5 6 7 1] Resize the array with length as length of array decremented by one i.e, (Lenght_of_array = len(array)- 1). After resizing = [2 3 4 5 6 7] Hence the first element is removed from the array.

#### Syntax of resize()

numpy.resize(arr, new_shape)

#### Parameters:

arr = The array to be passed to the function. new_shape = Shape of resized array.

#### Return:

Returns a array by resizing the array into specified shape

#### Approach

1. Import numpy library and create numpy array

2. Using the len() method to get the length of the given array

3. Rotate the array to left by one index.

4. Pass the array to the resize() and set the shape parameter to len(array)- 1

5. Print the array.

#### Source code

import numpy as np # creating numpy array arr = np.array([1,2,3,4,5,6,7]) lengthOfArray = len(arr) # Rotating the array to left by one index. firstElement = arr[0] for i in range(0,lengthOfArray-1): arr[i]=arr[i+1] arr[i+1]=firstElement # resizing the array using resize() arr.resize(lengthOfArray-1) print(arr)

#### OUTPUT:

[2 3 4 5 6 7]

#### Summary

Great! you made it, We have discussed the all possible methods to remove first element from a NumPy Array. Happy learning,

**Advertisements**

Thanks for reading.