Count occurrences

Python, List · Jan 9, 2021

Counts the occurrences of a value in a list.

  • Use list.count() to count the number of occurrences of val in lst.
def count_occurrences(lst, val):
  return lst.count(val)
count_occurrences([1, 1, 2, 1, 2, 3], 1) # 3

More like this

  • All indexes of value

    Returns a list of indexes of all the occurrences of an element in a list.

    Python, List · Oct 11, 2020

  • Count grouped elements

    Groups the elements of a list based on the given function and returns the count of elements in each group.

    Python, List · Nov 2, 2020

  • Filter non-unique list values

    Creates a list with the non-unique values filtered out.

    Python, List · Nov 2, 2020