Tally Votes with defaultdict

Easy ⏱ 8 min 77% acceptance ★★★★☆ 4.3
Write a function tally_votes(ballots) that takes a list of candidate-name strings and returns a collections.defaultdict(int) mapping each candidate to how many votes (occurrences) they received.

Examples

Example 1
Input
tally_votes(['A', 'B', 'A', 'C', 'B', 'A'])
Output
{'A': 3, 'B': 2, 'C': 1}
Explanation

Count each candidate name.

Constraints

  • Must use `collections.defaultdict`.

Topics

Dictionariesdefaultdict

Companies

AmazonRazorpay

Hints

Hint 1

`from collections import defaultdict`; `tally = defaultdict(int)`; then `tally[name] += 1` for each ballot.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.