Count Occurrences of Items in a Python List Using Comprehension

(Last Updated On: 09/03/2023)

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

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

lis = ['a', 'd', 'd', 'c', 'a', 'b', 'b', 'a', 'c', 'd', 'e']
occurrence = {item: lis.count(item) for item in lis}
print(occurrence.get('d'))

Output:

3

Leave a Reply

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