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: –


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

Output: –


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

Output: –


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} 

Output: –


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 *