Python List count() Method
Returns the number of times the given element occurs in the list.
Syntax
Python
Copy Code
list.count(value)
Parameters
Parameter | Description |
---|---|
value |
Required. The value to search for within the list. This value can be of any type |
Example
Python
Copy Code
groceries = ['apple', 'orange', 'paper towels', 'apple'] numApples = groceries.count('apple') numOranges = groceries.count('orange') print(f'We have {numApples} apples and {numOranges} oranges.')
Output
We have 2 apples and 1 oranges.