Good job! Now let's analyze a slightly modified example of counting elements.
Suppose we have a list with game results. Each person can take part in one or more games and win from 0 to 10 points in each game. The list presents all points that the participants collected:
game_results = [
('John', 9), ('Mike', 8), ('Anne', 5), ('Anne', 10),
('Kate', 8), ('Kate', 7), ('John', 3), ('Anne', 6)
]
Our task is to sum up the points for each person and present the results as a dictionary. Take a look:
dict_results = {}
for result in game_results:
name, points = result
dict_results[name] = dict_results.get(name, 0) + points
print(dict_results)
Result:
{'John': 12, 'Mike': 8, 'Anne': 21, 'Kate': 15}
As you can see, the code is very similar to the previous exercise. The only difference is that we don't add 1 each time. Instead, we add the number of points which we unpacked from the result tuple.