Drill

ProblemsPython › data

How many of each

easydataPython

A survey tallies how often each answer was given.

group_sizes(values: list<string>) → map<string, int>

Solve it in the editor →

Where you start

def group_sizes(values: list[str]) -> dict[str, int]:
    

Worked examples

CallResult
group_sizes(["a", "b", "a"]){"a": 2, "b": 1}
group_sizes(["x"]){"x": 1}
group_sizes(["p", "p", "p"]){"p": 3}
group_sizes(["a", "b", "c"]){"a": 1, "b": 1, "c": 1}

Hint

Add one to the map entry for each value you meet.

Reference solution in Python
def group_sizes(values: list[str]) -> dict[str, int]:
    result = {}
    for v in values:
        result[v] = result.get(v, 0) + 1
    return result

The same problem in another language

More data problems in Python