Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Count occurrences of a value in a list

Count how many times a given value appears in a list.

Python practice6 minLists & TuplesBeginnerLast updated March 17, 2026

Problem statement

Given a list and a target value, return the number of times the target occurs in the list. The list may contain elements of any type (ints, strings, None, etc.). The comparison should be the standard equality comparison (==).

Task

Write a function that returns the number of times a target value appears in a list.

Examples

Basic example

Input

count_occurrences([1, 2, 1, 3, 1], 1)

Output

3

The value 1 appears three times in the list.

Input format

A list (first argument) and a value to count (second argument).

Output format

An integer representing how many times the value appears in the list.

Constraints

The list length can be from 0 up to typical memory limits. Aim for O(n) time.

Samples

Sample 1

Input

count_occurrences(['a', 'b', 'a'], 'a')

Output

2

The string 'a' appears twice.