Signup/Sign In
PUBLISHED ON: JULY 9, 2021

Python program to find the sum of all items in a dictionary

In this tutorial, we will learn to find the sum of all items in a dictionary in Python. Dictionaries hold values in a key-value pair format, where each value has its own unique key. For a given dictionary with values, we have to find the sum of all items in the dictionary and print the result.

Look at the example to understand the input-output format.

Input: {'k1 ': 120, 'k2' : 20, 'k3' : 300}

Output: 440

Input: {'a' : 200, 'b' : 360, 'c' : 550}

Output: 1110

To solve this problem in Python, we can use the following approaches:

  1. By using sum() and values()
  2. By using the iterative method

Approach 1: sum() and values

In this approach, we will be using a combination of the sum() method and values() method of the dictionary class.

The sum() method will return the sum of all values in the dictionary. To get the values that are in the dictionary, we will be using the values() method. values() returns a list of all the values that are in the dictionary.

Syntax- dictionary_name.values()

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise a dictionary with keys and values

Step 2- Print the original dictionary

Step 3- Get the values using values() method

Step 4- Print the sum using the sum() method

Python Program 1

Look at the program to understand the implementation of the approach. We have declared a dictionary with keys and values. The values() method is passed in the sum() method because then all the values returned by the values() method will be added by the sum() method.

# sum of values in dictionary
#initialisation
dic={ 'x':455, 'y':223, 'z':300, 'p':908 }

print("Dictionary: ", dic)

#using sum() and values()
print("sum: ",sum(dic.values()))


Dictionary: {'x': 455, 'y': 223, 'z': 300, 'p': 908}
sum: 1886

Approach 2: The iterative approach

In this approach, we will be using a for loop to iterate through each item in the dictionary and add those items using a sum variable. To iterate through each value of the dictionary, we will be using the values() function and keep on adding it to the sum variable.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will accept dictionary and return the sum

Step 2- Initialise a sum variable that will store the sum of values

Step 3- Run a loop and add each value of the dictionary to the sum variable

Step 4- Return the sum variable

Step 5- Initialise a dictionary with keys and values

Step 6- Pass the dictionary in the defined function

Step 7- Print the value returned by the function

Python Program 2

Look at the program to understand the implementation of the approach. We have defined a function that will accept the dictionary and then add each value of the dictionary using a loop and a variable.

# Sum of values of a dictionary

# function
def Sum(dic):
    #sum variable
    sum=0
    #iterate through values
    for i in dic.values():
        sum=sum+i
    return sum

#initialisation
dic={ 'x':30, 'y':145, 'z':55 }

print("Dictionary: ", dic)

#print sum
print("sum: ",Sum(dic))


Dictionary: {'x': 30, 'y': 145, 'z': 55}
sum: 230

Conclusion

In this tutorial, we have seen two approaches for finding the sum of all items in a dictionary. We have seen how to use the sum() method for adding all the values. Also, we have used the iterative approach and calculated the sum using a loop and a variable that will store the result.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.