{"id":10396,"date":"2021-09-30T14:00:24","date_gmt":"2021-09-30T08:30:24","guid":{"rendered":"https:\/\/python-programs.com\/?p=10396"},"modified":"2021-11-22T18:34:36","modified_gmt":"2021-11-22T13:04:36","slug":"python-program-to-sum-all-the-items-in-a-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-sum-all-the-items-in-a-dictionary\/","title":{"rendered":"Python Program to Sum All the Items in a Dictionary"},"content":{"rendered":"
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples<\/a> and try to code all of them on your own then check with the exact code provided by expert programmers.<\/p>\n Dictionaries in Python:<\/strong><\/p>\n A dictionary, in a nutshell, is a collection of data stored in key\/value pairs. The keys in a dictionary must be immutable data types (such as text, integer, or tuple), whereas the contents in a dictionary can be any Python data type. Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Given a dictionary, the task is to print the sum of all the values in the given dictionary in Python.<\/p>\n There are several ways to calculate the sum of all the values in the given dictionary some of them are:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The total sum of all the given dictionary values is printed.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The total sum of all the given dictionary values is printed.<\/p>\n Related Programs<\/strong>:<\/p>\n Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers. Dictionaries in Python: A dictionary, in a nutshell, is a collection of …<\/p>\n
\nDuplicate keys are not permitted. If the same key is used twice in the same dictionary, the last occurrence takes precedence over the first.
\nBecause dictionary data can be changed, they are referred to as mutable objects. They are unordered, which indicates that the order we set for the elements is not preserved. (They are ordered starting with version 3.7.)
\nThey can grove or shrink as needed since they are dynamic.<\/p>\ngiven dictionary = {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38, \r\n 'coding': 10, 'students': 81}<\/pre>\n
The sum of all the values in the given dictionary\r\n {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81} :\r\n789<\/pre>\n
given dictionary = {'good': 38, 'morning': 293, 'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98,\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81}<\/pre>\n
The sum of all the values in the given dictionary\r\n {'good': 38, 'morning': 293, 'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38,\r\n 'coding': 10, 'students': 81} :\r\n1120<\/pre>\n
Program to Sum All the Items in a Dictionary in Python<\/h2>\n
\n
Method #1:Using sum function<\/h3>\n
\n
# Take a dictionary and initialize it with some random key and value pairs.\r\ngivendiction = {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98,\r\n 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81}\r\n# values() function returns the tuple of all the values present in the given dictionary.\r\n# Store this tuple in some variable.\r\nvalLst = givendiction.values()\r\n# Calculate the sum of the values list using the sum() function.\r\nsumvalu = sum(valLst)\r\n# Print the sum of all the values in the given dictionary.\r\nprint('The sum of all the values in the given dictionary\\n', givendiction, ':')\r\nprint(sumvalu)\r\n<\/pre>\n
The sum of all the values in the given dictionary\r\n {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81} :\r\n789<\/pre>\n
\n
Method #2:Using for loop<\/h3>\n
\n
# Take a dictionary and initialize it with some random key and value pairs.\r\ngivendiction = {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98,\r\n 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81}\r\n# Take a variable say cntValu which stores the sum of all the values in the dictionary.\r\n# Initialize the value of cntValu to 0.\r\ncntValu = 0\r\n# Traverse the dictionary using for loop.\r\nfor key in givendiction:\r\n # For every key add the value to cntValue.\r\n cntValu = cntValu + givendiction[key]\r\n# Print cntValu( sum of all the values in the given dictionary).\r\nprint('The sum of all the values in the given dictionary\\n', givendiction, ':')\r\nprint(cntValu)\r\n<\/pre>\n
The sum of all the values in the given dictionary\r\n {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81} :\r\n789<\/pre>\n
Method #3:Using for loop and items() function.<\/h3>\n
\n
# Take a dictionary and initialize it with some random key and value pairs.\r\ngivendiction = {'good': 38, 'morning': 293, 'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98,\r\n 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81}\r\n# Take a variable say cntValu which stores the sum of all the values in the dictionary.\r\n# Initialize the value of cntValu to 0.\r\ncntValu = 0\r\n# Traverse the dictionary using for loop and items() function..\r\nfor key, value in givendiction.items():\r\n # For every key add the value to cntValue.\r\n cntValu = cntValu + value\r\n# Print cntValu( sum of all the values in the given dictionary).\r\nprint('The sum of all the values in the given dictionary\\n', givendiction, ':')\r\nprint(cntValu)\r\n<\/pre>\n
The sum of all the values in the given dictionary\r\n {'good': 38, 'morning': 293, 'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81} :\r\n1120<\/pre>\n
\n