Python Program to print sum of all key value pairs in a Dictionary


In Python, dictionary is a implementation of a data structure known more commonly as an associative array. A dictionary is made up of a group of key-value pairs. Each key-value combination corresponds to a key and its corresponding value.

In this article, we will learn a python program to print the sum of all key-value pairs in a dictionary.

Methods Used

The following are the various methods to accomplish this task:

  • Using For Loop and append() function(dictionary traversal method)

  • Using dict.keys() Function

  • Using For Loop, keys() and values() Functions

Example

Assume we have taken an input dictionary. We will now print the sum of the key, and value pairs of each item of an input dictionary using the above methods.

Input

inputDict = {5: 3, 10: 2, 6: 4, 12:8}

Output

8 12 10 20 

In the above input dictionary, the sum of keys and values of each item are:

1st item − 5+3 = 8

2nd item − 10+2=12

3rd item − 6+4=10

4th item − 12+8 = 20

Hence we get the output as 8 12 10 20.

Using For Loop and append() function

In this method, we are going to use combination of simple for loop and append function to print sum of all key value pairs in a dictionary.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a function getKeyValuesSum() that prints the sum of key and value pairs of a dictionary by accepting the input dictionary as an argument.

  • Initialize an empty list to store the sum of keys and values of each dictionary item.

  • Use the for loop to traverse through each item of the input dictionary.

  • Add the key and value of the dictionary and append it to the result list.

  • Traverse in the result and print the sum of key-value pairs.

  • Create a variable to store the input dictionary.

  • Call the above-defined getKeyValuesSum() function by passing the input dictionary as an argument and then print it.

Example

The following program returns the sum of the key, and value pairs of each item of an input dictionary using for loop and append() function –

# creating a function that prints the sum of key and value pairs
# of a dictionary by accepting input dictionary as an argument
def getKeyValuesSum(inputDict):
    # Creating a list to store sum of keys and values of each dictionary item
    resultantList = []
    # traversing through each item of the input dictionary
    for item in inputDict:
        # Appending the key(item) as well as the value(inputDict[item]) to the result
        resultantList.append(item + inputDict[item])
    print("The Sum of key and value pairs of the dictionary is:")
    # Printing the resultant list
    for i in resultantList:
        print(i,end=' ')
# input dictionary
inputDict = {5: 3, 10: 2, 6: 4, 12: 8}
# Printing input dictionary
print("The Given Dictionary:", inputDict)
# calling the above defined getKeyValuesSum() function
# by passing input dictionary as an argument
getKeyValuesSum(inputDict)

Output

On executing, the above program will generate the following output –

The Given Dictionary: {5: 3, 10: 2, 6: 4, 12: 8}
The Sum of key and value pairs of the dictionary is:
8 12 10 20 

Using dict.keys() Function

In this method we are going to use key() function the dict. keys() method provides a view object that displays a list of all the keys in the dictionary in order of insertion.

Example

The following program returns the sum of the key, and value pairs of each item of an input dictionary using the keys() function –

def getKeyValuesSum(inputDict):
    # storing the sum of keys and values of each dictionary item
    resultantList = []
    # traversing through the keys of the input dictionary
    for k in inputDict.keys():
        # Appending the key as well as the value(inputDict[key]) to the result
        resultantList.append(k + inputDict[k])
    # Print the values of the resultant list
    print(resultantList)
# input dictionary
inputDict = {5: 3, 10: 2, 6: 4, 12: 8}
getKeyValuesSum(inputDict)

Output

On executing, the above program will generate the following output –

[8, 12, 10, 20]

Using For Loop, keys() and values() Functions

In this method we are going ot use key() and values() function to print sum ofall key value pairs in a python dictionary.

Syntax

keys() function:

the dict.keys() method provides a view object that displays a list of all the keys in the dictionary in order of insertion.

values() function: 

the dict.values() method provides a view object that displays a list of all the values in the dictionary in order of insertion.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input dictionary.

  • Use the dict.keys() function to get the list of all the keys of an input dictionary.

  • Use the dict.values() function to get the list of all the values of an input dictionary.

  • Initialize an empty list to store the sum of keys and values of each dictionary item.

  • Use the for loop to traverse in a range till the length of the keys list with the len() function(returns the number of items in an object).

  • Get the sum of current elements in both keys and value list and append it to the result list using the append() function.

  • Print the resultant list.

Example

The following program returns the sum of the key, and value pairs of each item of an input dictionary using the for Loop, keys() and values() functions –

# input dictionary
inputDict = {5: 3, 10: 2, 6: 4, 12: 8}
# getting a list of keys of an input dictionary
keysList = list(inputDict.keys())
# getting a list of values of an input dictionary
valuesList = list(inputDict.values())
# storing the sum of keys and values of each dictionary item in a list
resultantList = []
# traversing in a range till the length of the keys list
for p in range(0, len(keysList)):
  # appending the sum of current elements in both keys and value list and
    resultantList.append(keysList[p]+valuesList[p])
for e in resultantList:
  # printing current element
    print(e, end=" ")

Output

On executing, the above program will generate the following output –

8 12 10 20 

Conclusion

In this article, we have learned how to print the sum of all key-value pairs in a Dictionary using 3 different approaches. We also learned how to utilize the functions keys() and values() to get all the keys and dictionary values, respectively.

Updated on: 18-Aug-2023

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements