Given a dictionary and a value K, extract keys whose summation of values equals K.
Input : {“Gfg” : 3, “is” : 5, “Best” : 9, “for” : 8, “Geeks” : 10}, K = 17
Output : [‘Best’, ‘for’]
Explanation : 9 + 8 = 17, hence those keys are extracted.Input : {“Gfg” : 3, “is” : 5, “Best” : 9, “for” : 8, “Geeks” : 10}, K = 19
Output : [‘Best’, ‘Geeks’]
Explanation : 9 + 10 = 19, hence those keys are extracted.
Method #1 : Using loop
This is one of the ways in which this task can be performed. In this, we iterate for all the keys, and next keys in inner list, and keep on checking values summation. If its equal to K. The keys are stored.
Python3
# Python3 code to demonstrate working of # Dictionary Keys whose Values summation equals K # Using loop # initializing dictionary test_dict = {"Gfg" : 3, "is" : 5, "Best" : 9, "for" : 8, "Geeks" : 10} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # initializing K K = 14 # extracting keys and values keys = list(test_dict.keys()) values = list(test_dict.values()) res = Nonefor i in range(len(keys)): for j in range(i + 1, len(keys)): # checking if values equals K if values[i] + values[j] == K: res = [keys[i], keys[j]] # printing result print("Keys whose sum equals K : " + str(res)) |
The original dictionary is : {'Gfg': 3, 'is': 5, 'Best': 9, 'for': 8, 'Geeks': 10}
Keys whose sum equals K : ['is', 'Best']
Method #2 : Using list comprehension
This is yet another way in which this task can be performed. In this, we perform task similar to above method but in shorthand manner using list comprehension.
Python3
# Python3 code to demonstrate working of # Dictionary Keys whose Values summation equals K # Using list comprehension # initializing dictionary test_dict = {"Gfg" : 3, "is" : 5, "Best" : 9, "for" : 8, "Geeks" : 10} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # initializing K K = 14 # extracting keys and values keys = list(test_dict.keys()) values = list(test_dict.values()) # checking for keys in one liner res = [[keys[i], keys[j]] for i in range(len(keys)) for j in range(i + 1, len(keys)) if values[i] + values[j] == K] # printing result print("Keys whose sum equals K : " + str(res)) |
The original dictionary is : {'Gfg': 3, 'is': 5, 'Best': 9, 'for': 8, 'Geeks': 10}
Keys whose sum equals K : [['is', 'Best']]
Recommended Posts:
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Summation of Custom nested keys in Dictionary
- Python | Summation of dictionary list values
- Python - Summation of tuple dictionary values
- Python - Dictionary values String Length Summation
- Python - Nested Dictionary values summation
- Python - Product and Inter Summation dictionary values
- Python - Sort Dictionary by Values Summation
- Python - Extract selective keys' values Including Nested Keys
- Python | Split dictionary keys and values into separate lists
- Python program to Swap Keys and Values in Dictionary
- Python | Find keys with duplicate values in dictionary
- Python | Combine two dictionary adding values for common keys
- Python | Test if dictionary contains unique keys and values
- Python - Assign values to initialized dictionary keys
- Python - Unique value keys in a dictionary with lists as values
- Python - Keys associated with Values in Dictionary
- Python - Sort Dictionary by Values and Keys
- Python | Equal Keys List Summation
- Python | Selective Keys Summation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

