Skip to main content

How to Sum a Column’s Values in a Python List

You have a list of dictionaries and you would like to iterate over the data to sum the values of one particular key in each dictionary. This can be done easily using a list comprehension:

sales = [
    {'product': 'CHINESE Burning Blue', 'price': 152.25}, 
    {'product': 'CHINESE Burning Charms', 'price': 100.00}, 
    {'product': 'Crystal BEADED', 'price': 16.75}, 
    {'product': 'Crystal Ring', 'price': 78.00}, 
]
total_price = sum([sale['price'] for sale in sales])
print(total_price)
# output: 347.0
data = [[1, 1, 1],
        [2, 2, 2],
        [3, 4, 5]]
totals = map(sum, zip(*data))
print(list(totals)) # [7, 7, 8]

By continuing to use the site, you agree to the use of cookies.