Python Program to Sum All the Items in a Dictionary

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 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.
Duplicate keys are not permitted. If the same key is used twice in the same dictionary, the last occurrence takes precedence over the first.
Because 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.)
They can grove or shrink as needed since they are dynamic.

Examples:

Example1:

Input:

given dictionary = {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38, 
                                 'coding': 10, 'students': 81}

Output:

The sum of all the values in the given dictionary
 {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81} :
789

Example2:

Input:

given dictionary = {'good': 38, 'morning': 293, 'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98,
                                'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81}

Output:

The sum of all the values in the given dictionary
 {'good': 38, 'morning': 293, 'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38,
 'coding': 10, 'students': 81} :
1120

Given a dictionary, the task is to print the sum of all the values in the given dictionary in Python.

Program to Sum All the Items in a Dictionary in Python

There are several ways to calculate the sum of all the values in the given dictionary some of them are:

Method #1:Using sum function

Approach:

  • Take a dictionary and initialize it with some random key and value pairs.
  • values() function returns the tuple of all the values present in the given dictionary.
  • Store this tuple in some variable.
  • Calculate the sum of the values list using the sum() function.
  • Print the sum of all the values in the given dictionary.
  • The Exit of the Program.

Below is the implementation:

# Take a dictionary and initialize it with some random key and value pairs.
givendiction = {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98,
                'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81}
# values() function returns the tuple of all the values present in the given dictionary.
# Store this tuple in some variable.
valLst = givendiction.values()
# Calculate the sum of the values list using the sum() function.
sumvalu = sum(valLst)
# Print the sum of all the values in the given dictionary.
print('The sum of all the values in the given dictionary\n', givendiction, ':')
print(sumvalu)

Output:

The sum of all the values in the given dictionary
 {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81} :
789

Explanation:

  • The sum() function computes the total of all the values in the given dictionary.
  • The total sum of all the given dictionary values is printed.

Method #2:Using for loop

Approach:

  • Take a dictionary and initialize it with some random key and value pairs.
  • Take a variable say cntValu which stores the sum of all the values in the dictionary.
  • Initialize the value of cntValu to 0.
  • Traverse the dictionary using for loop.
  • For every key add the value to cntValue.
  • Print cntValu ( sum of all the values in the given dictionary).
  • The Exit of the Program.

Below is the implementation:

# Take a dictionary and initialize it with some random key and value pairs.
givendiction = {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98,
                'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81}
# Take a variable say cntValu which stores the sum of all the values in the dictionary.
# Initialize the value of cntValu to 0.
cntValu = 0
# Traverse the dictionary using for loop.
for key in givendiction:
    # For every key add the value to cntValue.
    cntValu = cntValu + givendiction[key]
# Print cntValu( sum of all the values in the given dictionary).
print('The sum of all the values in the given dictionary\n', givendiction, ':')
print(cntValu)

Output:

The sum of all the values in the given dictionary
 {'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81} :
789

The total sum of all the given dictionary values is printed.

Method #3:Using for loop and items() function.

Approach:

  • Take a dictionary and initialize it with some random key and value pairs.
  • Take a variable say cntValu which stores the sum of all the values in the dictionary.
  • Initialize the value of cntValu to 0.
  • Traverse the dictionary using for loop and items() function.
  • Add the value to cntValue.
  • Print cntValu ( sum of all the values in the given dictionary).
  • The Exit of the Program.

Below is the implementation:

# Take a dictionary and initialize it with some random key and value pairs.
givendiction = {'good': 38, 'morning': 293, 'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98,
                'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81}
# Take a variable say cntValu which stores the sum of all the values in the dictionary.
# Initialize the value of cntValu to 0.
cntValu = 0
# Traverse the dictionary using for loop and items() function..
for key, value in givendiction.items():
    # For every key add the value to cntValue.
    cntValu = cntValu + value
# Print cntValu( sum of all the values in the given dictionary).
print('The sum of all the values in the given dictionary\n', givendiction, ':')
print(cntValu)

Output:

The sum of all the values in the given dictionary
 {'good': 38, 'morning': 293, 'heello': 27, 'this': 282, 'is': 98, 'BTechGeeks': 98, 'online': 123, 'platform': 32, 'for': 38, 'coding': 10, 'students': 81} :
1120

The total sum of all the given dictionary values is printed.

Related Programs: