Count Occurrences in List
Count how many times a given element occurs in a list.
BeginnerTopic: List Programs
Python Count Occurrences in List Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# 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
Understanding Count Occurrences in List
We use the list method .count(target) to count occurrences of a given value.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.