Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Question: Write a Python program to count the occurrences of each element in a list.
Answer:

from collections import Counter

def count_occurrences(lst):

  return Counter(lst)

Example:

count_occurrences([1, 2, 1, 3, 2, 4, 1])  # Output: {1: 3, 2: 2, 3: 1, 4: 1}
Is it helpful? Yes No

Most helpful rated by users:

©2025 WithoutBook