def countVotes(ballots): voters = {} candidates = {} for voter, candidate in ballots: # make sure vote has not already voted if (voter not in voters): voters[voter] = True # if this is the first vote for a candidate, initialize # the candidate's vote total to 0 if candidate not in candidates: candidates[candidate] = 0 candidates[candidate] += 1 finalists = sorted(candidates.iteritems(), key=itemgetter(1), reverse=True) for candidate, votes in finalists: print candidate, votes