Count Occurrences of Items in a Python List Using count()

(Last Updated On: 08/03/2023)

Python Program to Count Occurrences of Items in a Python List Using count()

# Count the Occurrence of an Item in a List Using count() in python

# Python code to count the number of occurrences
def countX(lst, x):
	return lst.count(x)

# Driver Code
lst = [1,4,5,7,4,9,1,6,1,3,6,8,1,9,8,4,1,1]
x = 1
print('{} has occurred {} times'.format(x,countX(lst, x)))

Output:

1 has occurred 6 times

Leave a Reply

Your email address will not be published. Required fields are marked *