Editor

Count the Votes

#28 · Dictionaries and sets · Beginner · 30 XP · about 5 min

Each ballot in an election is a candidate's name. Write tally_votes(ballots) that counts the votes and returns a dictionary mapping each name to its number of votes.

Put the names in the dictionary in the order they first appear in ballots. A blank ballot (the empty string '') is spoiled: do not count it. Names are counted exactly as written, so 'Ana' and 'ana' are different candidates.

Examples

tally_votes(['Ana', 'Ben', 'Ana'])             # returns {'Ana': 2, 'Ben': 1}
tally_votes(['Zoe', '', 'Max', 'Zoe', 'Zoe'])  # returns {'Zoe': 3, 'Max': 1}
tally_votes([])                                # returns {}

In the second example the blank ballot is skipped, and Zoe comes first because her name appeared first.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.