The Wayback Machine - http://web.archive.org/web/20240118165412/https://www.geeksforgeeks.org/python-operator-countof/
Open In App
Related Articles

Python | Operator.countOf

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Operator.countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value.

Syntax : Operator.countOf(freq = a, value = b)
Parameters :
freq : It can be list or any other data type which store value
value : It is value for which we have to count the number of occurrences

Returns: Count of number of occurrences of value.

Example #1: Use Operator.countOf() to count the number of occurrences of given value in a list.




# Python program showing
# use of countOf() function
# in a list
  
from operator import * 
arr = [ 1, 2, 3, 3, 3 ]
arr1 = [ 'a', 'b', 'c', 'b', 'd' ]
  
print("Number of occurrence of b in arr1 =", countOf(arr1, "b"))
print("Number of occurrence of e in arr1 =", countOf(arr1, "e"))
print("Number of occurrence of 3 in arr =", countOf(arr, 3))


Output:

Number of occurrence of b in arr1 = 2
Number of occurrence of e in arr1 = 0
Number of occurrence of 3 in arr = 3

 
Example #2: Use Operator.countOf() to count the number of occurrences of given value in a dictionary.




# Python program showing
# use of countOf() function
# in a dictionary
  
from operator import *
  
d = {"score": 3, "score1": 1, "score2": 3, "score3": 1, "score4": 3}
  
print("Number of occurrence of three in dict =",countOf(d.values(),3))
print("Number of occurrence of one in dict =",countOf(d.values(),1))
print("Number of occurrence of four in dict =",countOf(d.values(),4))


Output :

Number of occurrence of three in dict = 3
Number of occurrence of one in dict = 2
Number of occurrence of four in dict = 0

Example #3: Use Operator.countOf() to count the number of occurrences of given value in a tuples.




# Python program showing
# use of countOf() function
# in a tuples
  
from operator import *
   
tup = ( 1, 2, 3, 3, 3 )
tup1 = ( 'a', 'b', 'c', 'b', 'd' )
   
print("Number of occurrence of b in tuple1 =",countOf(tup1,"b"))
print("Number of occurrence of e in tuple1 =",countOf(tup1,"e"))
print("Number of occurrence of 3 in tuple =",countOf(tup,3))


Output :

Number of occurrence of b in tuple1 = 2
Number of occurrence of e in tuple1 = 0
Number of occurrence of 3 in tuple = 3

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 30 Sep, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials