List with repeated items
Input
count_items(['a', 'b', 'a'])
Output
{'a': 2, 'b': 1}
The function returns counts: 'a' appears twice and 'b' once. 'a' appears before 'b', so it comes first in the dictionary.
Full lesson preview
Count how many times each item appears in an iterable using a dictionary.
Problem statement
Task
Examples
Input
count_items(['a', 'b', 'a'])
Output
{'a': 2, 'b': 1}
The function returns counts: 'a' appears twice and 'b' once. 'a' appears before 'b', so it comes first in the dictionary.
Input format
Output format
Constraints
Samples
Input
count_items(['x', 'x', 'y'])
Output
{'x': 2, 'y': 1}
x appears twice and y once; x appears before y in the result because it appeared first in the input.