Count Occurrences in List

Count how many times a given element occurs in a list.

PythonBeginner
Python
# Program to count occurrences of an element in a list

items = input("Enter list elements separated by space: ").split()
target = input("Enter element to count: ")

print("Occurrences:", items.count(target))

Output

Enter list elements separated by space: a b a c a
Enter element to count: a
Occurrences: 3

We use the list method .count(target) to count occurrences of a given value.