You are here: Home / Topics / Python code to count the number of occurrences in list

Python code to count the number of occurrences in list

Filed under: Python on 2023-09-17 22:26:41

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Python code to count the number of occurrences


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


# Driver Code

lst = [
   8,
   6,
   8,
   10,
   8,
   20,
   10,
   8,
   8,
   ]
x = 8
print '{} has occurred {} times'.format(x, countX(lst, x))

About Author:
M
Mr. Dubey     View Profile
Founder and CEO of MCQ Buddy. I just like to help others. This portal helps students in getting study material free.