Operator.countOf in Python

In this tutorial, we are going to learn what is Operator.countOf function in Python. This function is used to count the occurrence of one element in another. The working parameters for this function are String and Integer. Its basic syntax is: –

Operator.countOf(x,y)

Where x is the list or any data storing object in which we have to find the frequency. Moreover, y is the value whose frequency we have to find. It will be easier to understand if you look at the following examples one by one. Here I have mentioned some applications of using this function.

Operator.countOf in List Python

In this, the x parameter is a list.

Code: –

import operator
lst=["Ad","Am","Ad","Am0","Qw","Ad"]
operator.countOf(lst,"Ad")

Output: –

3

As we see the String Ad appears 3 times in the list.

Operator.countOf in Tuples

In this, the x parameter is a tuple. And even the y parameter can be a tuple.

Code: –

import operator
x=(10,11,12,13,14,10,9,10)
y=(9)
operator.countOf(x,y)

Output: –

1

Operator.countOf in Dictionary

In this x parameter is a dictionary we can search in both the keys and the values.

Code: –

import operator
dict={"A": 4, "B": 4, "C": 4, "D": 4, "E": 4} 
operator.countOf(dict.values(),4)

Output: –

5

These are the various ways it can be used.

Also Read: –

List and Dictionary Manipulation in Python

Leave a Reply

Your email address will not be published. Required fields are marked *