Count Occurrences of Items in a Python List Using Loop

(Last Updated On: 08/03/2023)

Python Program to Count Occurrences of Items in a List Using Loop

# Count the Occurrence of an Item in a List Using a Loop in python

def countX(lst, x):
	count = 0
	for ele in lst:
		if (ele == x):
			count = count + 1
	return count

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


1 has occurred 4 times

Leave a Reply

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