Python Program to Remove the first element from an Array


In order to remove the first element of the array, the index that must be considered is 0 as the index of the first element in any array is always 0. As like removing the last element from an array, removing the first element from the array can be processed using the same techniques.

Let us apply those techniques in the deletion of first element of the array. We shall discuss the methods and keywords that are used to remove the first element from an array one after another in a row now.

Using the pop() Method

The method pop() is used to delete the elements of the arrays, lists, etc in Python programming language. This mechanism works by using the index of the element which must be removed or deleted from the array.

So, to remove the first element of the array, consider the index 0. The element simply pops up from the array and will be removed. The syntax of the “ pop() ” method is described below. Let us use the method and delete the first element of the array.

Syntax

arr.pop(0)

Example

In this example, we are going to discuss about the process of removing the first element of an array by using pop() method. The steps followed to construct such program are as follows −

  • Declare an array and define some elements within the array.

  • By using the pop() method, mention the first index of the array, i.e., 0 within the parenthesis of the method in order to delete the first element.

  • Print the array after deletion of the first element.

arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "]
first_index = 0

print(" The elements of the array before deletion: ")
print(arr)

print(" The elements of the array after deletion: ")
arr.pop(first_index)
print(arr)

Output

The output of the above program is as follows −

The elements of the array before deletion: 
[' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
The elements of the array after deletion:
[' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']

Using the del Keyword

The keyword del is used to delete the objects in Python. This keyword is also used in order to delete the last element of an array or any element by using its index. So, we use this keyword to delete specific objects or elements in Python. Following is the syntax of this keyword −

del arr[first_index]

Example

In the following example, we are going to discuss about the process of removing the first element of an array by using “ del ” keyword.

arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "]
first_index = 0
print(" The elements of the array before deletion: ")
print(arr)

print(" The elements of the array after deletion: ")
del arr[first_index]
print(arr)

Output

The output of the above program is as follows −

The elements of the array before deletion: 
[' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
The elements of the array after deletion:
[' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']

Using Delete() Method of Numpy Module

The method delete() can remove the element from the array when its index is clearly mentioned. In order to use the method delete(), the array should be converted into the form of Numpy Array. The conversion of normal array to a numpy array can also be performed by using the module. The syntax of the delete() method is described below.

Syntax

variable = n.delete(arr, first_index)

Example

In this example, we are going to discuss about the process of removing the first element of an array by using delete() method of Numpy module.

import numpy as n
arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "]
variable = n.array(arr)
first_index = 0
print(" The elements of the array before deletion: ")
print(variable)
variable = n.delete(arr, first_index)
print(" The elements of the array after deletion: ")
print(variable)

Output

The output of the above program is as follows −

The elements of the array before deletion: 
[' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
The elements of the array after deletion:
[' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']

Conclusion

We can clearly observe that the outputs of all three programs are same which tells us that the first element of the array is successfully removed from the array by using all three ways. In this way, the deletion of an element of any index in an array can performed very easily by using simple techniques. If the index of the element of the array is known by the user, then the process of deletion becomes very easy. If not the index, at least the value of the element must be known so that the “ remove() ” method can be applied.

Updated on: 08-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements