List Count method in Python

Spread the love

List Count –

The list count method returns the number of times an element appear in the list.

Syntax of List Count –

list.count(element)

element – count of the element that you are interested in

How to use count –


In [1]: nums = [1, 6, 1, 3, 1, 6, 8, 1] 

In [2]: # how many times 1 appears in the list

In [3]: nums.count(1)
Out[3]: 4

In [4]: # how many times 6 appears in the list

In [5]: nums.count(6)
Out[5]: 2

In [6]: # how many times 100 appears in the list

In [7]: nums.count(100)
Out[7]: 0

Counting Lists and tuples inside a List –

In [10]: nums = [5, [10,20], [10,20],(5, 10)]

In [11]: nums.count([10,20])
Out[11]: 2

In [12]: nums.count((5,10))
Out[12]: 1

In [13]: nums.count((20,20))
Out[13]: 0

Rating: 1 out of 5.

Leave a Reply