Skip to content
Related Articles

Related Articles

Python – Selective Key Values Summation
  • Last Updated : 03 Jul, 2020

Sometimes, while working with Python dictionaries, we can have a problem in which we desire to get summation of certain keys’ values in dictionary. This kind of application can have usecase in many domains such as day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_dict = {‘Gfg’ : 4, ‘is’ : 2, ‘best’ : 7}, key_list = [‘Gfg’, ‘best’]
Output : 11

Input : test_dict = {‘Gfg’ : 4, ‘best’ : 7}, key_list = [‘Gfg’]
Output : 4

Method #1 : Using loop
This is one of the ways in which this task can be performed. In this, we iterate for target list keys and sum the corresponding values from dictionary.




# Python3 code to demonstrate working of 
# Selective Key Values Summation
# Using loop
  
# initializing dictionary
test_dict = {'Gfg' : 4, 'is' : 2, 'best' : 7, 'for' : 9, 'geeks' : 10
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing keys_list
key_list = ['Gfg', 'best', 'geeks']
  
# Selective Key Values Summation
# Using loop
res = 0  
for key in key_list:
    res += test_dict[key]  
          
# printing result 
print("The keys summation : " + str(res)) 
Output :



The original dictionary is : {'Gfg': 4, 'is': 2, 'best': 7, 'for': 9, 'geeks': 10}
The keys summation : 21

 

Method #2 : Using sum() + list comprehension
The combination of above functions can be used to solve this problem. In this, we perform summation using sum() and list comprehension is used to perform task of iteration.




# Python3 code to demonstrate working of 
# Selective Key Values Summation
# Using sum() + list comprehension
  
# initializing dictionary
test_dict = {'Gfg' : 4, 'is' : 2, 'best' : 7, 'for' : 9, 'geeks' : 10
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing keys_list
key_list = ['Gfg', 'best', 'geeks']
  
# Selective Key Values Summation
# Using sum() + list comprehension
res = sum([test_dict[key] for key in key_list])
  
# printing result 
print("The keys summation : " + str(res)) 
Output :
The original dictionary is : {'Gfg': 4, 'is': 2, 'best': 7, 'for': 9, 'geeks': 10}
The keys summation : 21

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :