Files
30-seconds-of-code/snippets/count_occurences.md
Sreejith S 4f6aa69f26 Fixed a grammatical error.
Changed  'an list' to 'a list' in 5th line.
2018-10-18 15:43:05 +05:30

17 lines
419 B
Markdown

### count_occurences
:information_source: Already implemented via `list.count()`.
Counts the occurrences of a value in a list.
Uses the list comprehension to increment a counter each time you encounter the specific value inside the list.
```python
def count_occurrences(lst, val):
return len([x for x in lst if x == val and type(x) == type(val)])
```
```python
count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3
```