Open In App

Python – Extract Dictionary Items with Summation Greater than K

Last Updated : 27 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given dictionary with value lists, extract all those items with values summing over K.

Input : {“Gfg” : [6, 3, 4], “is” : [8, 10, 12], “Best” : [10, 16, 14], “for” : [7, 4, 3], “geeks” : [1, 2, 3, 4]}, K = 10 
Output : {“Gfg” : [6, 3, 4], “is” : [8, 10, 12], “Best” : [10, 16, 14], “for” : [7, 4, 3], “geeks” : [1, 2, 3, 4]} 
Explanation : All elements are greater than 10.

Input : {“Gfg” : [6, 3, 4], “is” : [8, 10, 12], “Best” : [10, 16, 14], “for” : [7, 4, 3], “geeks” : [1, 2, 3, 4]}, K = 50 
Output : {} 
Explanation : No elements are greater than 50.

Method #1 :  Using dictionary comprehension + sum()

This is one of the ways in which this problem can be solved. In this one-liner, we iterate through the keys and append the dictionary item only if its value’s total crosses K computed using sum().

Python3




# Python3 code to demonstrate working of
# Extract Dictionary Items with Summation Greater than K
# Using dictionary comprehension + sum()
 
# initializing dictionary
test_dict = {"Gfg" : [6, 3, 4],
             "is" :  [8, 10, 12],
             "Best" : [10, 16, 14], 
             "for" : [7, 4, 3],
             "geeks" : [1, 2, 3, 4]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 15
 
# summation using sum(), values extracted using items()
res = {key: val for key, val in test_dict.items() if sum(val) > K}
 
# printing result
print("The computed dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': [6, 3, 4], 'is': [8, 10, 12], 'Best': [10, 16, 14], 'for': [7, 4, 3], 'geeks': [1, 2, 3, 4]}
The computed dictionary : {'is': [8, 10, 12], 'Best': [10, 16, 14]}

Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

Method #2 : Using filter() + lambda() + sum() + dict()

This is another way in which this task can be performed. In this, computation part is done using filter() and lambda, summation using sum() and result converted to dictionary using dict().

Python3




# Python3 code to demonstrate working of
# Extract Dictionary Items with Summation Greater than K
# Using filter() + lambda() + sum() + dict()
 
# initializing dictionary
test_dict = {"Gfg" : [6, 3, 4],
             "is" :  [8, 10, 12],
             "Best" : [10, 16, 14], 
             "for" : [7, 4, 3],
             "geeks" : [1, 2, 3, 4]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 15
 
# summation using sum(), values extracted using items()
# filter() + lambda used for computation
res = dict(filter(lambda ele: sum(ele[1]) > K, test_dict.items()))
 
# printing result
print("The computed dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': [6, 3, 4], 'is': [8, 10, 12], 'Best': [10, 16, 14], 'for': [7, 4, 3], 'geeks': [1, 2, 3, 4]}
The computed dictionary : {'is': [8, 10, 12], 'Best': [10, 16, 14]}

Method #3: Using for loop and if statement

  • Initialize the dictionary test_dict with the given key-value pairs.
  • Print the original dictionary.
  • Initialize the value of K.
  • Create an empty dictionary res to store the computed dictionary.
  • Loop through the key-value pairs in test_dict.
  • For each key-value pair, compute the sum of the values using sum(value).
  • Check if the sum is greater than K.
  • If the sum is greater than K, add the key-value pair to res.
  • Print the computed dictionary.

Python3




# initializing dictionary
test_dict = {"Gfg" : [6, 3, 4],
             "is" :  [8, 10, 12],
             "Best" : [10, 16, 14], 
             "for" : [7, 4, 3],
             "geeks" : [1, 2, 3, 4]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 15
 
# initializing result dictionary
res = {}
 
# loop through the dictionary and check if the sum of the values is greater than K
for key, value in test_dict.items():
    if sum(value) > K:
        res[key] = value
 
# printing result
print("The computed dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': [6, 3, 4], 'is': [8, 10, 12], 'Best': [10, 16, 14], 'for': [7, 4, 3], 'geeks': [1, 2, 3, 4]}
The computed dictionary : {'is': [8, 10, 12], 'Best': [10, 16, 14]}

Time complexity: O(n), where n is the number of key-value pairs in the dictionary
Auxiliary space: O(k), where k is the number of key-value pairs in the result dictionary (in the worst case, all key-value pairs in the original dictionary satisfy the condition and are included in the result dictionary)

Method #4: Using the reduce() function from the functools module

Step-by-step approach:

Import the functools module.
Initialize the dictionary and K value as given in the problem statement.
Use the reduce() function to iterate through the dictionary and check if the sum of the values is greater than K. If yes, add the key-value pair to the resulting dictionary.
Print the resulting dictionary.

Python3




# import the functools module
import functools
 
# initializing dictionary
test_dict = {"Gfg" : [6, 3, 4],
             "is" :  [8, 10, 12],
             "Best" : [10, 16, 14], 
             "for" : [7, 4, 3],
             "geeks" : [1, 2, 3, 4]}
 
# initializing K
K = 15
 
# using reduce() to iterate through the dictionary and filter out key-value pairs whose sum of values is greater than K
res = functools.reduce(lambda acc, item: dict(list(acc.items()) + [(item, test_dict[item])]) if sum(test_dict[item]) > K else acc, test_dict.keys(), {})
 
# printing result
print("The computed dictionary : " + str(res))


Output

The computed dictionary : {'is': [8, 10, 12], 'Best': [10, 16, 14]}

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.

Auxiliary space: O(1) for the variables used in the code, and O(n) for the resulting dictionary



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads