Python program to find the sum of dictionary keys
Last Updated :
14 Mar, 2023
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
test_dict = { 3 : 4 , 9 : 10 , 15 : 10 , 5 : 7 , 6 : 7 }
print ( "The original dictionary is : " + str (test_dict))
res = 0
for key in test_dict:
res + = key
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
test_dict = { 3 : 4 , 9 : 10 , 15 : 10 , 5 : 7 , 6 : 7 }
print ( "The original dictionary is : " + str (test_dict))
res = sum ( list (test_dict.keys()))
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
from functools import reduce
test_dict = { 3 : 4 , 9 : 10 , 15 : 10 , 5 : 7 }
print ( "The original dictionary is : " + str (test_dict))
res = reduce ( lambda x, b: x + b, test_dict)
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
test_dict = { 3 : 4 , 9 : 10 , 15 : 10 , 5 : 7 }
print ( "The original dictionary is : " + str (test_dict))
res = sum ([key for key in test_dict])
print ( "The dictionary keys summation : " + str (res))
|
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
test_dict = { 3 : 4 , 9 : 10 , 15 : 10 , 5 : 7 }
print ( "The original dictionary is : " + str (test_dict))
res = sum (test_dict.keys())
print ( "The dictionary keys summation : " + str (res))
|
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:
- Initialize a variable res to 0
- 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
- Print res
Python3
test_dict = { 3 : 4 , 9 : 10 , 15 : 10 , 5 : 7 }
print ( "The original dictionary is : " + str (test_dict))
res = 0
while len (test_dict) > 0 :
key, value = test_dict.popitem()
res + = key
print ( "The dictionary keys summation : " + str (res))
|
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).
Similar Reads
Python program to find the sum of all items in a dictionary
The task of finding the sum of all items in a dictionary in Python involves calculating the total of all values stored in a dictionary. For example, given a dictionary {'a': 100, 'b': 200, 'c': 300}, the sum of values would be 100 + 200 + 300 = 600. Using sum()This is the simplest and fastest method
3 min read
Python Program to Find Sum of Array
Given an array of integers, find the sum of its elements. Examples: Input : arr[] = {1, 2, 3}Output : 6Explanation: 1 + 2 + 3 = 6This Python program calculates the sum of an array by iterating through each element and adding it to a running total. The sum is then returned. An example usage is provid
4 min read
Python program to find the sum of a Sublist
Given a list of numbers, write a Python program to find the sum of all the elements in the list. Examples: Input: arr = [2,4,5,10], i = 1, j = 3Output: 19Input: arr = [4,10,5,3,3], i = 3, j = 3Output: 3Find the sum of a Sublist Applying Brute Force In this method, we will be initializing an output v
3 min read
Python Program to find XOR of all the key value pairs in a Dictionary
Given a dictionary in python, write a program to find the XOR of all the key-value pairs in the dictionary and return in the form of an array. Note: All the keys and values in the dictionary are integers. Examples: Input : dic={1:3, 4:5, 6:7, 3 :8}Output : [2, 1, 1, 11]Explanation: XOR of all the ke
3 min read
Python program to multiply all the items in a dictionary
Python program to illustrate multiplying all the items in a dictionary could be done by creating a dictionary that will store all the key-value pairs, multiplying the value of all the keys, and storing it in a variable. Example: Input: dict = {'value1':5, 'value2':4, 'value3':3, 'value4':2, 'value5'
2 min read
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 33Explanation: Sum of key and value of the first item in the dictionary = 1 + 10
5 min read
Python program to find Cumulative sum of a list
Calculating the cumulative sum of a list means finding the running total of the elements as we move through the list. In this article, we will explore How to find the cumulative sum of a list. Using itertools.accumulate()This is the most efficient method for calculating cumulative sums. itertools mo
3 min read
Output of Python Programs | Set 24 (Dictionary)
Prerequisite : Python-Dictionary1. What will be the output? [GFGTABS] Python dictionary = {"geek":10, "for":45, "geeks": 90} print("geek" in dictionary) [/GFGTABS]Options: 10FalseTrueErrorOutput:3. TrueExplanation: in is used to check the key exist in dictiona
2 min read
Python | Value summation of key in dictionary
Many operations such as grouping and conversions are possible using Python dictionaries. But sometimes, we can also have a problem in which we need to perform the aggregation of values of key in dictionary list. This task is common in day-day programming. Let's discuss certain ways in which this tas
6 min read
Python | Reversed Order keys in dictionary
While working with dictionary, we can sometimes, have a problem in which we require to print dictionaries in the reversed order of their occurrence. Let's discuss certain ways in which this problem can be solved. Method #1 : Using reversed() + sorted() + keys() + loop The combination of above functi
4 min read