Introduction
In Python, it is often necessary to count the number of occurrences of a specific value in a list. This can be useful in a variety of situations, such as analyzing data or processing user input. In this challenge, you will be tasked with writing a function that counts the number of occurrences of a given value in a list.
Count Occurrences
Write a function count_occurrences(lst, val) that takes a list lst and a value val as arguments and returns the number of occurrences of val in lst. Your function should use the built-in list.count() method to count the number of occurrences.
def count_occurrences(lst, val):
return lst.count(val)
count_occurrences([1, 1, 2, 1, 2, 3], 1) ## 3
Summary
In this challenge, you learned how to count the number of occurrences of a value in a list using the list.count() method. This is a useful technique that can be applied in a variety of programming tasks.