Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using for, if and dictionary comprehension solution in Clear category for Convert and Aggregate by H0r4c3
def conv_aggr(data: list[tuple[str, int]]) -> dict[str, int]:
data_dict = dict()
for key, value in data:
if key in data_dict.keys():
print(f'Key {key} does exist in dictionary')
data_dict[key] += value
else:
print(f'Key {key} does not exist in dictionary')
data_dict[key] = value
# Check that the key is not an empty string and the value is not 0
data_dict = {key:value for key, value in data_dict.items() if key and value}
print(data_dict)
return data_dict
print("Example:")
print(conv_aggr([("a", 7), ("b", 8), ("a", 10)]))
assert conv_aggr([("a", 7), ("b", 8), ("a", 10)]) == {"a": 17, "b": 8}
assert conv_aggr([]) == {}
assert conv_aggr([("a", 5), ("a", -5)]) == {}
assert conv_aggr([("a", 5), ("a", 5), ("a", 0)]) == {"a": 10}
assert conv_aggr([("a", 5), ("", 15)]) == {"a": 5}
print("The mission is done! Click 'Check Solution' to earn rewards!")
Aug. 21, 2022