Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Count Item Frequencies

Return a dictionary mapping each distinct item to how many times it appears in a list.

Python practice15 minLoops & IterationIntermediateLast updated March 15, 2026

Problem statement

Implement a function that takes a list of items and returns a dictionary where each key is an item from the list and its value is the number of times that item appears. Maintain the order of first appearance for keys (the natural behavior of Python dict in modern versions will satisfy this). The function should handle empty lists and arbitrary hashable items (numbers, strings, tuples, booleans). This exercise reinforces looping, dictionary usage, and counting patterns.

Task

Practice iterating over a sequence and building a frequency table (dictionary) to count occurrences of items.

Examples

Count numbers

Input

[1, 2, 1, 3, 2]

Output

{1: 2, 2: 2, 3: 1}

1 appears twice, 2 appears twice, 3 appears once.

Input format

A single list of hashable items passed to count_frequencies(items).

Output format

A dictionary mapping each distinct item to its count (int).

Constraints

The list length will be between 0 and 10,000. Items are hashable. Aim for O(n) time and O(k) additional space where k is number of distinct items.

Samples

Sample 1

Input

["a", "b", "a"]

Output

{'a': 2, 'b': 1}

Counts for each string item.