Very good! After calculating the total donations on specific days, we want to calculate the total donations for specific locations. Things are going to get a bit more complicated because we are essentially calculating sums for "columns" instead of "rows". Take a look:
donations = [
[345.0, 287.80, 119.27, 329.30],
[294.25, 349.0, 178.90, 262.34],
[401.0, 456.45, 289.43, 319.27]
]
place_sums = [0, 0, 0, 0]
for i, line in enumerate(donations):
for j, value in enumerate(line):
place_sums[j] += value
print(place_sums)
This time, we need to prepare a list with temporary values that will store column sums. Because we need an access to indexes, we used another kind of for
loop – one that iterates over the indexes and values using the enumerate()
function. The outer loop iterates over the rows of the list, while the inner loop iterates over values in each row. Inside the inner loop, we add the current value from the nested list (value
) to the respective column sum (place_sums[j]
).