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

Given a dictionary arr consisting of N items, where key and value are both of integer type, the task is to find the sum of all key value pairs in the dictionary.

Examples:

Input: arr = {1: 10, 2: 20, 3: 30}
Output: 11 22 33
Explanation:
Sum of key and value of the first item in the dictionary = 1 + 10 = 11.
Sum of key and value of the second item in the dictionary = 2 + 20 = 22.
Sum of key and value of the third item in the dictionary = 3 + 30 = 33.

Input: arr = {10 : -5, 5 : -10, 100 : -50}
Output: 5 -5 50

Method 1:

Approach using dictionary traversal technique: The idea is to traverse through the keys of dictionary using for loop. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## Python3

 `# Python3 implementation of` `# the above approach`   `# Function to print the list containing` `# the sum of key and value pairs of` `# each item of a dictionary`     `def` `FindSum(arr):`   `    ``# Stores the list containing the` `    ``# sum of keys and values of each item` `    ``l ``=` `[]`   `    ``# Traverse the dictionary` `    ``for` `i ``in` `arr:`   `        ``l.append(i ``+` `arr[i])`   `    ``# Print the list l` `    ``print``(``*``l)`   `# Driver Code`     `arr ``=` `{``1``: ``10``, ``2``: ``20``, ``3``: ``30``}`   `FindSum(arr)`

Output

`11 22 33`

Time Complexity: O(N)
Auxiliary Space: O(N)

Method 2:

Approach using keys() Method: An alternate approach to solve the problem is to use keys() method. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## Python3

 `# Python3 implementation of the above approach`   `# Function to print the list` `# containing the sum of key and` `# value pairs from a dictionary`     `def` `FindSum(arr):`   `    ``# Stores the list containing the` `    ``# sum of keys and values of each item` `    ``l ``=` `[]`   `    ``# Traverse the list of keys of arr` `    ``for` `i ``in` `arr.keys():`   `        ``l.append(i ``+` `arr[i])`   `    ``# Print the list l` `    ``print``(``*``l)`   `# Driver Code`     `arr ``=` `{``1``: ``10``, ``2``: ``20``, ``3``: ``30``}`   `FindSum(arr)`

Output

`11 22 33`

Time Complexity: O(N)
Auxiliary Space: O(N)

Method 3 : Using keys(),values() and for loop

## Python3

 `# Python3 implementation of the above approach` `arr ``=` `{``1``: ``10``, ``2``: ``20``, ``3``: ``30``}` `x ``=` `list``(arr.keys())` `y ``=` `list``(arr.values())` `res ``=` `[]` `for` `i ``in` `range``(``0``, ``len``(x)):` `    ``res.append(x[i]``+``y[i])` `for` `i ``in` `res:` `    ``print``(i, end``=``" "``)`

Output

`11 22 33 `

Time Complexity: O(n), where n is the number of keys in the dictionary.
Auxiliary Space: O(n), as two arrays of size n are created to store the keys and values of the dictionary.

Method 4: Using zip() and a list comprehension

This approach uses the python built-in function zip() to extract the keys and values of the dictionary and combines them in a tuple. Using a list comprehension, we add the key and value of each item in the dictionary. The final result is the sum of all key-value pairs in the dictionary. The time complexity of this approach is O(n) and the space complexity is O(n) as we are creating a new list to store the sum of each key-value pair.

## Python3

 `def` `find_sum(arr):` `    ``# Using zip to get the keys and values of the dictionary` `    ``keys, values ``=` `zip``(``*``arr.items())` `    ``# Using list comprehension to add the key and value of each item` `    ``return` `[k``+``v ``for` `k,v ``in` `zip``(keys, values)]`   `arr ``=` `{``1``: ``10``, ``2``: ``20``, ``3``: ``30``}` `print``(find_sum(arr))` `#This code is contributed by Edula Vinay Kumar Reddy`

Output

`[11, 22, 33]`

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 5: Using a simple for loop to iterate over the keys and values of the dictionary

Directly iterate over the items of the dictionary using the items() method, which returns a sequence of (key, value) pairs. We then add each key and value together and append the result to a list. Finally, we return the list of results.

## Python3

 `def` `find_sum(arr):` `    ``# Create an empty list to store the result` `    ``result ``=` `[]` `    `  `    ``# Iterate over the keys and values of the dictionary` `    ``for` `key, value ``in` `arr.items():` `        ``# Add the key and value and append the result to the list` `        ``result.append(key ``+` `value)` `    `  `    ``# Return the list of results` `    ``return` `result`   `# Example usage:` `arr ``=` `{``1``: ``10``, ``2``: ``20``, ``3``: ``30``}` `print``(find_sum(arr))` `# Output: [11, 22, 33]`

Output

`[11, 22, 33]`

Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(n), since we are creating a list to store the result.

Method 6:Using itertools strmap()

Algorithm:

1. Import the itertools.starmap() function
2. Define the find_sum() function that takes a dictionary arr as input
3. Use the starmap() function with a lambda function to add the key and value of each item in the dictionary arr
4. Convert the resulting iterable of sums to a list and return the list

## Python3

 `from` `itertools ``import` `starmap`   `def` `find_sum(arr):` `    ``return` `list``(starmap(``lambda` `key, value: key``+``value, arr.items()))`   `# example usage` `arr ``=` `{``1``: ``10``, ``2``: ``20``, ``3``: ``30``}` `result ``=` `find_sum(arr)` `print``(result)  ``# Output: [11, 22, 33]` `#This code is contributed by Vinay pinjala.`

Output

`[11, 22, 33]`

Time complexity:
The time complexity of the find_sum() function is O(n), where n is the number of items in the dictionary arr. The starmap() function iterates over each item in the dictionary and applies a lambda function to calculate the sum of the key and value for each item. The time complexity of the lambda function is constant, so it does not affect the overall time complexity of the function.

Auxiliary Space:

Auxiliary Space of the code is O(1),because we don’t storing list.

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next