Home >Backend Development >Python Tutorial >Python program to delete the first element in 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's apply these techniques to remove the first element of an array. Now, we will discuss the methods and keywords for removing the first element from an array one by one.
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 delete the first element of the array, consider index 0. The elements are simply popped out of the array and removed. The syntax of the "pop()" method is described below. Let us use this method to delete the first element of the array.
arr.pop(0)
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 in the array.
Remove the first element by using the pop() method by mentioning the first index of the array i.e. 0 inside the brackets of the method.
Print the array after removing 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)
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 ']
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]
In the following example, we will discuss the process of deleting the first element of an array using the "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)
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 ']
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.
variable = n.delete(arr, first_index)
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)
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 ']
We can clearly observe that the output of these three programs is the same, which tells us that by using these three methods, the first element was successfully removed from the array. By using simple techniques, it is very easy to delete an element at any index position in an array. If the user knows the index of the array element, the deletion process becomes very simple. If the index is not known, at least the value of the element must be known so that the "remove()" method can be applied.
The above is the detailed content of Python program to delete the first element in an array. For more information, please follow other related articles on the PHP Chinese website!