How to Count occurrences of an item in a list in python
from collections import Counter
1ist1 =['Peter','Rose','Donald','Peter']
a = Counter(listl).get('Peter')print(f'Peter appears in the list {a} times')# Output:# Peter appears in the list 2 times
"""Find all occurrences of element in list"""# If you simply want find how many times an element appears# use the built-in function count()deffind_occur(lst, item):return lst.count(item)# Test -----------------------------------------------------------print(find_occur([None,None,1,2,3,4,5],None))# 2# If you wanna find where they occur instead# - This returns the indices where element is found within a listdeffind(lst, item):return[i for(i, x)inenumerate(lst)if x == item]# Test Code ------------------------------------------------------from random import randint, choice
lst =[randint(0,99)for x inrange(10)]# random lst
item = choice(lst)# item to find
found = find(lst, item)# lst of where item is found atprint(f"lst: {lst}",f"item: {item}",f"found: {found}",
sep = "
")
program to count the number of occurrences of a elementes in a list python
#Count the frequency of elements in a list using dictionary
l=eva1.l(input("Enter the list"))
d={}print(l)for i in l:if i notin d:
d[i]=l.count(i)else:passprint("Frequency of element:")for i in d:print(i,"-",d[i])
output:
Enter the list[1,5,8,7,5,6,3,2,4,7,5,1,][1,5,8,7,5,6,3,2,4,7,5,1]
Frequency of element:1-25-38-17-26-13-12-14-1