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
1# Program to count occurrences of an element in a list23items = input("Enter list elements separated by space: ").split()4target = input("Enter element to count: ")56print("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.
After this program
If the idea is still fuzzy, read Day 5: Lists and List Methods in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 list programs, or see every topic on Python exercises.