Warning: session_start(): open(/tmp/sess_60ba805aab9c6c5e5a1939541b6775f2, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Sum The Values In A List Of Dictionaries In Python - LearnShareIT

How To Sum The Values In A List Of Dictionaries In Python

Sum the values in a list of dictionaries in Python

How can you sum the values ​​in a list of dictionaries in Python? If you still have questions, we will introduce you some methods, such as list comprehension and map function,… Read the following article for more details.

Sum the values ​​in a list of dictionaries in Python

Use the sum, dict.values function and list comprehension

Example:

  • Create a list of dictionaries.
  • Use the dict.values() and list comprehension functions to find dictionary values.
  • Convert those values ​​into a list and sum them using the sum function.
dictInfor = [
    {"Id" : 111, "wages" : 100}, 
    {"Id" : 112, "wages" : 200},
    {"Id" : 113, "wages" : 300},
    {"Id" : 114, "wages" : 100},
    {"Id" : 115, "wages" : 200}
]

# Sum the values in list myInf
total = sum([sum(list(i.values())) for i in dictInfor])

print('Sum the values in the dictionary:', total)

Output:

Sum the values in the dictionary: 1465

Or you can use list comprehension as follows.

Example:

dictInfor = [
    {"Id" : 111, "wages" : 100}, 
    {"Id" : 112, "wages" : 200},
    {"Id" : 113, "wages" : 300},
    {"Id" : 114, "wages" : 100},
    {"Id" : 115, "wages" : 200}
]

total1 = sum(item['Id'] for item in dictInfor)
total2 = sum(item['wages'] for item in dictInfor)

print('Sum the values in the dictionary:', total1 + total2)

Output:

Sum the values in the dictionary: 1465

Use for loop

Example:

  • Create a list of dictionaries.
  • Use a nested for loop to find the dictionary’s values and sum them.
dictInfor = [  
    {"Id" : 111, "wages" : 100}, 
    {"Id" : 112, "wages" : 200},
    {"Id" : 113, "wages" : 300},
    {"Id" : 114, "wages" : 100},
    {"Id" : 115, "wages" : 200}
]

total = 0 

# Use the for loop
for i in dictInfor:
    for a in i:
        total += i[a]
        
print('Sum the values in the dictionary:', total)

Output:

Sum the values in the dictionary: 1465

Use the map function.

In this way, we will use the map function with the first parameter (The function to execute for each element in the iterable) being the operator.itemgetter function.

Syntax:

operator.itemgetter( item)

The operator.itemgetter function returns a callable object to fetch an item from its operand using the getitem method. 

Example:

  • Create a list of dictionaries.
  • In the example above, the map() function will convert the value corresponding to the two keys ‘Id’ and ‘Wages’ of the dictionary into an object that can be called to fetch the item from its operand using the getitem method. (This happens because the first parameter of the map function is the operator.itemgetter function).
  • Then perform the sum using the sum function.
import operator

dictInfor = [  
    {"Id" : 111, "wages" : 100}, 
    {"Id" : 112, "wages" : 200},
    {"Id" : 113, "wages" : 300},
    {"Id" : 114, "wages" : 100},
    {"Id" : 115, "wages" : 200}
]

totalId = sum(map(operator.itemgetter('Id'),dictInfor))
totalWages = sum(map(operator.itemgetter('wages'),dictInfor))

print('Sum the values in the dictionary:', totalId + totalWages)

Output:

Sum the values in the dictionary: 1465

Summary

Those are three ways we use to sum the values in a list of dictionaries in Python. You can use any methods that work for you. Please comment below if you have any questions, and we will answer you. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *