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

419 B
Raw Blame History

count_occurences

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.

def count_occurrences(lst, val):
    return len([x for x in lst if x == val and type(x) == type(val)])
count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3