Merge pull request #336 from psyonara/better_count_occurences

Use Python's built-in count method to do the counting for us.
This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-11 10:29:40 +03:00
committed by GitHub

View File

@ -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 `list.count()` to count the number of occurrences of `val` in `lst`.
```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