Python count List Items

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

list_name.count(list_item)

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

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))
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, and then we used the count function on it.

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'))
Total Number of Times 'Apple' has repeated =  3
Total Number of Times 'Banana' has repeated =  2

Let me use this Python count function on Mixed List.

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))
Total Number of Times 'Apple' has repeated =  3
Total Number of Times 10 has repeated =  4

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

This time, we used the count function on the Nested list (one inside the another).

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

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

In this example, we sued both the string and integer lists to count the number of times an item repeated.


Fruits = ['Apple', 'Orange', 'Banana', 'Kiwi', 'Apple', 'Kiwi']
numbers = [9, 4, -5, 0, 9, -1, 4, 9]

print("Original : ", Fruits)

#Count of List items
print('Number of Times Kiwi is repeated = ', Fruits.count('Kiwi'))
print('Number of Times Apple is repeated = ', Fruits.count('Apple'))
print('Number of Times Banana is repeated ', Fruits.count('Banana'))

print("\nOriginal number : ", numbers)
print('Number of Times 9 is repeated = ', numbers.count(9))
print('Number of Times 4 is repeated = ', numbers.count(4))
print('Number of Times 0 is repeated = ', numbers.count(0))
Original :  ['Apple', 'Orange', 'Banana', 'Kiwi', 'Apple', 'Kiwi']
Number of Times Kiwi is repeated =  2
Number of Times Apple is repeated =  2
Number of Times Banana is repeated  1

Original number :  [9, 4, -5, 0, 9, -1, 4, 9]
Number of Times 9 is repeated =  3
Number of Times 4 is repeated =  2
Number of Times 0 is repeated =  1

List count Function Example 2

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

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

count function Example 3

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

strCountList = []
 
number = int(input("Please enter the Total Number of 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))
Please enter the Total Number of 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