What is remove() method from array in python?

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.

Syntax

array.remove(x)

Parameter

  • x: The specified value whose first instance is removed from the array.

Return value

This function does not return anything.

Example code

#import module
from array import array
#initialize array with integer values
array_one = array("i", [1, 2, 3, 4, 5, 3, 6])
#initialize array with double values
array_two = array("d", [6.4, 2.2, 8.7, 2.2])
#initialize array with double values
array_three = array("d", [5.72, 7.48, 0.43])
#initialize array with signed values
array_four = array("b", [-7, +8, -10, +14, -7, -7])
print (array_one)
#remove first occurence of 3
array_one.remove(3)
print(array_one)
print ("\n")
print (array_two)
#remove first occurence of 2.2
array_two.remove(2.2)
print(array_two)
print ("\n")
print (array_three)
#remove first occurence of 7.48
array_three.remove(7.48)
print(array_three)
print ("\n")
print (array_four)
#remove first occurence of -7
array_four.remove(-7)
print(array_four)

Explanation

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.

Free Resources