Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What is count() method from array in Python?

Eman Kashif


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 count(x) function counts the specified value x occurrences.


counts = array.count(x)


  • x: The specified value to be counted in the array.

Return value

This function returns the number of occurrences of x.


Let’s look at the code below:

#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])

#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)
#find count of 3
print("Count of 3:",array_one.count(3))
print ("\n")

print (array_two)
#find count of 2.2
print("Count of 2.2:",array_two.count(2.2))
print ("\n")

print (array_three)
#find count of 0.2
print("Count of 0.2:",array_three.count(0.2))
print ("\n")

print (array_four)
#find count of -7
print("Count of -7:",array_four.count(-7))


In the code above:

  • Line 2: We import the array module.

  • Line 5: We initialize an array with data type integer in two arrays with data type double in lines 8 and 11, and an array with data type signed integer in line 14.

  • We then print each array and the count of a particular value.

  • Line 18: We print the count of 3 in array_one by passing 3 as a parameter in the array.count() function.

  • Line 23: We print the count of 2.2 in array_two.

  • Line 28: We print the count of 0.2 in array_three.

  • Line 33: We print the count of -7 in array_four.



View all Courses

Keep Exploring