Use Python's built-in count method to do the counting for us.
This commit is contained in:
@ -5,11 +5,11 @@ tags: list,beginner
|
||||
|
||||
Counts the occurrences of a value in a list.
|
||||
|
||||
- Increment a counter for every item in the list that has the given value and is of the same type.
|
||||
- Use the `count` method of Python's list object to count the number of occurrences.
|
||||
|
||||
```py
|
||||
def count_occurrences(lst, val):
|
||||
return len([x for x in lst if x == val and type(x) == type(val)])
|
||||
return lst.count(val)
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
Reference in New Issue
Block a user