Python List count()

Previous
Next

In this tutorial, we will see about Python List‘s count method.Python List count method is used to count a number of instances of the element in the list.

Python List count example

You can simply use count method to find number of occurrences of element in the list.
Let’s understand this with the help of simple example.

list1=[1,2,3,2,3,2,2,3,4,4,1,1,2] print("Count of 1 in the list1:",list1.count(1)) print("Count of 2 in the list1:",list1.count(2)) print("Count of 3 in the list1:",list1.count(3)) print("Count of 4 in the list1:",list1.count(4))

Output:

Count of 1 in the list1: 3
Count of 2 in the list1: 5
Count of 3 in the list1: 3
Count of 4 in the list1: 2

You can also find count of set or tuple too.Let’s see with the help of example.

list1=['one',{1,2},'three',{1,2}] print('Count of set {1,2}:',list1.count({1,2})) list2=['one',(3,4),'three',(3,4),(3,4)] print('Count of tuple (3,4) :',list2.count((3,4)))

Output:

Count of set {1,2}: 2
Count of tuple (3,4) : 3

That’s all about Python List count method.

Previous
Next

Add Comment