Count an item in a list #
The list.count() method in Python is used to count the number of occurrences of a specific item in a list. It takes a single argument, which is the item to be counted.
For example:
fruits = ['apple', 'banana', 'cherry', 'apple', 'orange', 'apple']
count = fruits.count('apple')
print(count) # Output: 3
Exercise #
Use the method count() to count the number of "cherry" in the list and assign to the variable count
Tests #
- Variable
countshould be the number of"cherry"in the listfruits - Use
count()function - Do not edit the first line of code
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules