Count Occurrences in List in Python

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

BeginnerList ProgramsExample 4 of 25
count-occurrences-in-list.py
Run in browser
1# Program to count occurrences of an element in a list
2
3items = input("Enter list elements separated by space: ").split()
4target = input("Enter element to count: ")
5
6print("Occurrences:", items.count(target))

Output

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

What's going on

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