Files
30-seconds-of-code/snippets/count_occurences.md
2018-02-26 10:00:18 +08:00

417 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