Files
30-seconds-of-code/snippets/count_occurences.md
2018-04-12 15:36:12 +05:30

420 B
Raw Blame History

count_occurences

Already implemented via list.count().

Counts the occurrences of a value in an 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