Thursday, December 26, 2024
Google search engine
HomeLanguagesPython program to find the sum of dictionary keys

Python program to find the sum of dictionary keys

Given a dictionary with integer keys. The task is to find the sum of all the keys.

Examples:

Input : test_dict = {3 : 4, 9 : 10, 15 : 10, 5 : 7} 
Output : 32 
Explanation : 3 + 9 + 15 + 5 = 32, sum of keys.

Input : test_dict = {3 : 4, 9 : 10, 15 : 10} 
Output : 27 
Explanation : 3 + 9 + 15 = 27, sum of keys. 

Method #1: Using loop

This is one of the ways in which this task can be performed. In this, we iterate all the keys in the dictionary and compute summation using a counter.

Python3




# Python3 code to demonstrate working of
# Dictionary Keys Summation
# Using loop
 
# initializing dictionary
test_dict = {3: 4, 9: 10, 15: 10, 5: 7, 6: 7}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
res = 0
for key in test_dict:
 
    # adding keys
    res += key
 
# printing result
print("The dictionary keys summation : " + str(res))


Output

The original dictionary is : {3: 4, 9: 10, 15: 10, 5: 7, 6: 7}
The dictionary keys summation : 38

Method #2 : Using keys() + sum()

This is shorthand with the help of which this task can be performed. In this, we extract all keys in list using keys(), and the summation is performed using sum().

Python3




# Python3 code to demonstrate working of
# Dictionary Keys Summation
# Using keys() + sum()
 
# initializing dictionary
test_dict = {3: 4, 9: 10, 15: 10, 5: 7, 6: 7}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# sum() performs summation
res = sum(list(test_dict.keys()))
 
# printing result
print("The dictionary keys summation : " + str(res))


Output

The original dictionary is : {3: 4, 9: 10, 15: 10, 5: 7, 6: 7}
The dictionary keys summation : 38

Method #3: Using reduce( )

This is shorthand with the help of which this task can be performed. In this, we sum all the keys value using lambda function in reduce.

Python




# Python3 code to demonstrate working of
# Dictionary Keys Summation
# Using reduce
from functools import reduce
 
# initializing dictionary
test_dict = {3: 4, 9: 10, 15: 10, 5: 7}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
res = reduce(lambda x, b: x + b, test_dict)
 
# printing result
print("The dictionary keys summation : " + str(res))


Output

The original dictionary is : {9: 10, 3: 4, 5: 7, 15: 10}
The dictionary keys summation : 32

Method #4: Using list comprehension

This approach uses a list comprehension to extract all the keys from the dictionary and then uses the built-in sum function to find the sum of all the keys.

Python3




# Python3 code to demonstrate working of
# Dictionary Keys Summation
# Using list comprehension
 
# initializing dictionary
test_dict = {3: 4, 9: 10, 15: 10, 5: 7}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
res = sum([key for key in test_dict])
 
# printing result
print("The dictionary keys summation : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original dictionary is : {3: 4, 9: 10, 15: 10, 5: 7}
The dictionary keys summation : 32

Time complexity: O(n) 
Auxiliary space: O(n) where n is the number of keys in the dictionary.

Method #5: Using the built-in function dict.keys() and sum()

Use the dict.keys() function to extract all the keys in the dictionary and then use the built-in sum() function to calculate the sum of all the keys.

Python3




# Python3 code to demonstrate working of
# Dictionary Keys Summation
# Using dict.keys() and sum()
 
# initializing dictionary
test_dict = {3: 4, 9: 10, 15: 10, 5: 7}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
res = sum(test_dict.keys())
 
# printing result
print("The dictionary keys summation : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original dictionary is : {3: 4, 9: 10, 15: 10, 5: 7}
The dictionary keys summation : 32

Time complexity: O(n) where n is the number of keys in the dictionary. 
Auxiliary space: O(n) as we are creating a new list of size n to store all the keys of the dictionary

Method #6:Using while loop() and popitem()

Algorithm:

  1. Initialize a variable res to 0
  2. While the length of test_dict is greater than 0, do the following:
    a. Remove the last key-value pair from test_dict using the popitem() method
    b. Add the key of the removed pair to res
  3. Print res

Python3




test_dict = {3: 4, 9: 10, 15: 10, 5: 7}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res = 0
while len(test_dict) > 0:
    key, value = test_dict.popitem()
    res += key
 
# printing result
print("The dictionary keys summation : " + str(res))
#This code is contributed by Vinay pinjala.


Output

The original dictionary is : {3: 4, 9: 10, 15: 10, 5: 7}
The dictionary keys summation : 32

Time complexity:
The time complexity of this algorithm depends on the size of the dictionary test_dict. In the worst case, the loop will execute n times, where n is the number of key-value pairs in the dictionary. The popitem() method has a time complexity of O(1), so the total time complexity of the loop is O(n). Therefore, the time complexity of the entire algorithm is O(n).

Auxiliary Space:
The space complexity of this algorithm depends on the size of the dictionary test_dict and the size of the variables res, key, and value. The space complexity of the variables is O(1). Therefore, the total space complexity of the algorithm O(1).

RELATED ARTICLES

Most Popular

Recent Comments