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])
# 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. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.