count() function of array class

The count() function counts the number of times an element is present in an array object. The Python example creates an array of signed integers and counts the number of times the integer 3 occurs in the array.

Array Updated 13 Jan 2020 1 min read

Method Name

count

Signature

count(elem)

Return Value

The number of times an element elem is present in an array object.

Overview

  • The count() method returns the number of occurrences of an element in the array.

Example

python
# Example Python program that counts the number of occurrences of a specific

# element present in an array

import array

# Create an array of signed integers

numbers = array.array('i');

# Add elements to the array

numbers.append(1);

numbers.append(2);

# Add 3 twice

numbers.append(3);

numbers.append(3);

# Count the number of occurrences of 3

num2Find = 3;

occurences = numbers.count(num2Find);

print("Number of occurrences of %d:"%num2Find);

print(occurences);

Output

Output
Number of occurrences of 3:

2