Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
using defaultdict solution in Clear category for Aggregate and Count by oduvan
from collections import defaultdict
def aggregate_and_count(items: list) -> dict:
ret = defaultdict(int)
for name, total in items:
ret[name] += total
return ret
print("Example:")
print(aggregate_and_count([["a", 1], ["b", 2], ["c", 3], ["a", 5]]))
assert aggregate_and_count([["a", 1], ["b", 2]]) == {"a": 1, "b": 2}
assert aggregate_and_count([["a", 1], ["a", 2]]) == {"a": 3}
assert aggregate_and_count([["a", 1], ["b", 2], ["c", 3], ["a", 5]]) == {
"a": 6,
"b": 2,
"c": 3,
}
assert aggregate_and_count([["a", 1]]) == {"a": 1}
print("The aggregation is done! Click 'Check' to earn cool rewards!")
July 17, 2021
Comments: