Python count List Items

Python count List function is used to count how many times an item repeated in a given list. The syntax of the list count function is

list_name.count(list_item)

The python count function counts the total number of times the item repeated in a given list. The below code count 10 and 20 in an integer list.

# Python Count List Items

a = [10, 20, 30, 10, 40, 10, 50, 20]

print("Total Number of Times 10 has repeated = ", a.count(10))
print("Total Number of Times 20 has repeated = ", a.count(20))

List count function output

Total Number of Times 10 has repeated =  3
Total Number of Times 20 has repeated =  2

Python count List Example

In this example, we declared a string list. Next, we used the count function on it.

TIP: Please refer to List and List methods articles in Python.

# Python Count List Items

Fruits = ['Apple', 'Orange', 'Banana', 'Apple', 'Grape', 'Banana', 'Apple']

print("Total Number of Times 'Apple' has repeated = ", Fruits.count('Apple'))
print("Total Number of Times 'Banana' has repeated = ", Fruits.count('Banana'))

List count function output

Total Number of Times 'Apple' has repeated =  3
Total Number of Times 'Banana' has repeated =  2

Let me use this count function on Mixed List.

# Python Count List Items

MxList = ['Apple', 10, 'Banana', 10, 'Apple', 'Grape', 10, 30, 10, 50, 'Apple']

print("Total Number of Times 'Apple' has repeated = ", MxList.count('Apple'))
print("Total Number of Times 10 has repeated = ", MxList.count(10))

Count list items output

Total Number of Times 'Apple' has repeated =  3
Total Number of Times 10 has repeated =  4

This time, we used the List count function on the Nested list (list inside a list).

# Python Count List Items

MxList = [[10, 20], [20, 30], [10, 20], [40, 50], [10, 80]]

print("Total Number of Times [10,20] has repeated = ", MxList.count([10,20]))

List count function output

Total Number of Times [10,20] has repeated =  2

List count Function Example 2

This python program allows the user to enter the List length and For Loop to append those numbers to the list. Then we used the list count function to count the list item 10.

# Python Count List Items

intCountList = []
 
number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    intCountList.append(value)
    
item = int(input("Please enter the Item that you want to Count: "))
print("Total Number of Times has repeated = ", intCountList.count(item))
Python Count List Function 5

List count Function Example 3

This list count function program allows users to enter their own string or words and then count the word

# Python Count List Items

strCountList = []
 
number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = input("Please enter the Value of %d Element : " %i)
    strCountList.append(value)
    
item = input("Please enter the Item that you want to Count: ")
print("Total Number of Times has repeated = ", strCountList.count(item))

List count function output

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : Kiwi
Please enter the Value of 2 Element : Dragon
Please enter the Value of 3 Element : Banana
Please enter the Value of 4 Element : Kiwi
Please enter the Value of 5 Element : Apple
Please enter the Item that you want to Count: Kiwi
Total Number of Times has repeated =  2