How to Count the Occurrences of an Item in a List
To count how many times an item occurs in a list, you can use the count()
method.
Example: Counting Occurrences in a List
# Counting occurrences in a list
fruits = ["apple", "banana", "apple", "cherry", "apple"]
apple_count = fruits.count("apple")
print(f"'apple' occurs {apple_count} times in the list.")
Output
'apple' occurs 3 times in the list.
Explanation: The count()
method counts how many times 'apple' appears in the list, which is 3 in this case.