Python is a high-level programming language that provides functionalities for several operations. The array
module comes with various methods to manipulate array data structures.
The remove(x)
function removes the first instance of the specified value x
.
array.remove(x)
x
: The specified value whose first instance is removed from the array.This function does not return anything.
#import modulefrom array import array#initialize array with integer valuesarray_one = array("i", [1, 2, 3, 4, 5, 3, 6])#initialize array with double valuesarray_two = array("d", [6.4, 2.2, 8.7, 2.2])#initialize array with double valuesarray_three = array("d", [5.72, 7.48, 0.43])#initialize array with signed valuesarray_four = array("b", [-7, +8, -10, +14, -7, -7])print (array_one)#remove first occurence of 3array_one.remove(3)print(array_one)print ("\n")print (array_two)#remove first occurence of 2.2array_two.remove(2.2)print(array_two)print ("\n")print (array_three)#remove first occurence of 7.48array_three.remove(7.48)print(array_three)print ("\n")print (array_four)#remove first occurence of -7array_four.remove(-7)print(array_four)
In the code above:
In line 2, we import the array
module.
In line 5, we initialize an array with data type integer.
In lines 8 and 11, we initialize two arrays with data type double.
In line 14, we initialize an array with data type signed integer.
In lines 16-20, we print array_one
, remove the first occurrence of a particular value (3
) from it, and print it again.
We repeat the same steps for the other arrays, passing a different value to the remove
function each time, in lines 22-37.