How to count the occurrences of specific element in a list with Python

2 Answers

0 votes
def count_occurrences(lst, n): 
    return lst.count(n) 
  
lst = [5, 6, 3, 1, 2, 4, 3, 3, 8, 7, 3, 9, 3] 
n = 3

print(count_occurrences(lst, n)) 



'''
run:

5

'''

 



answered Dec 26, 2019 by avibootz
0 votes
from collections import Counter 
  
lst = [5, 6, 3, 1, 2, 4, 3, 3, 8, 7, 3, 9, 3] 
n = 3

c = Counter(lst) 
print(c[n]) 

print(Counter(lst)[n]) 


'''
run:

5
5

'''

 



answered Dec 26, 2019 by avibootz
...