Open In App

Python – Extract dictionaries with values sum greater than K

Improve
Improve
Like Article
Like
Save
Share
Report

Given a dictionaries list, extract all the dictionaries whose keys summation exceeds K.

Input : test_list = [{“Gfg” : 4, “is” : 8, “best” : 9}, {“Gfg” : 3, “is”: 7, “best” : 5}], K = 15 
Output : [{‘Gfg’: 4, ‘is’: 8, ‘best’: 9}] 
Explanation : 4 + 9 + 8 = 21. Greater than K, hence returned. 

Input : test_list = [{“Gfg” : 4, “is” : 8, “best” : 9}, {“Gfg” : 3, “is”: 7, “best” : 5}], K = 25 
Output : [] 
Explanation : No dictionary with sum > 25.

Method #1 : Using loop

This is a brute way in which this task can be performed. In this, we iterate for all the dictionaries and exact summation of each of them, which exceeds K, we filter them out.

Python3




# Python3 code to demonstrate working of
# Extract dictionaries with values sum greater than K
# Using
 
# initializing lists
test_list = [{"Gfg" : 4, "is" : 8, "best" : 9},
             {"Gfg" : 5, "is": 8, "best" : 1},
             {"Gfg" : 3, "is": 7, "best" : 6},
             {"Gfg" : 3, "is": 7, "best" : 5}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 15
 
# using loop to extract all dictionaries
res = []
for sub in test_list:
    sum = 0
    for key in sub:
        sum += sub[key]
    if sum > K:
        res.append(sub)
 
# printing result
print("Dictionaries with summation greater than K : " + str(res))


Output

The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}]
Dictionaries with summation greater than K : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 3, 'is': 7, 'best': 6}]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”. 

Method #2 : Using list comprehension + sum()

This is one liner way in which this task can be performed. In this, we perform the task of summation using sum() and list comprehension can be used to perform task of combining all the logic into single line.

Python3




# Python3 code to demonstrate working of
# Extract dictionaries with values sum greater than K
# Using list comprehension + sum()
 
# initializing lists
test_list = [{"Gfg" : 4, "is" : 8, "best" : 9},
             {"Gfg" : 5, "is": 8, "best" : 1},
             {"Gfg" : 3, "is": 7, "best" : 6},
             {"Gfg" : 3, "is": 7, "best" : 5}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 15
 
# using one-liner to extract all the dictionaries
res = [sub for sub in test_list if sum(list(sub.values())) > K]
 
# printing result
print("Dictionaries with summation greater than K : " + str(res))


Output

The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}]
Dictionaries with summation greater than K : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 3, 'is': 7, 'best': 6}]

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

Method #3 : Using the ‘filter()’ function with a ‘lambda’ function

Python3




# Python3 code to demonstrate working of
# Extract dictionaries with values sum greater than K
# Using filter() and lambda function
 
# initializing lists
test_list = [{"Gfg" : 4, "is" : 8, "best" : 9},
            {"Gfg" : 5, "is": 8, "best" : 1},
            {"Gfg" : 3, "is": 7, "best" : 6},
            {"Gfg" : 3, "is": 7, "best" : 5}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 15
 
# Using filter() and lambda function
result = list(filter(lambda x: sum(x.values()) > K, test_list))
 
# printing result
print("Dictionaries with summation greater than K : " + str(result))


Output

The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}]
Dictionaries with summation greater than K : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 3, 'is': 7, 'best': 6}]

Time complexity: O(n*m)
Auxiliary Space: O(n) 

Method #4: Using for loop and conditional statement

  • Initialize an empty list to store the dictionaries that meet the condition.
  • Loop through the dictionaries in the test_list using a for loop.
  • Calculate the sum of the values of the current dictionary using the sum() function and store the result in a variable.
  • Check if the sum is greater than K using a conditional statement.
  • If the condition is True, append the current dictionary to the list of dictionaries that meet the condition.
  • Return the list of dictionaries that meet the condition.

Python3




# initializing lists
test_list = [{"Gfg" : 4, "is" : 8, "best" : 9},
            {"Gfg" : 5, "is": 8, "best" : 1},
            {"Gfg" : 3, "is": 7, "best" : 6},
            {"Gfg" : 3, "is": 7, "best" : 5}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 15
 
# Using for loop and conditional statement
result = []
for d in test_list:
    if sum(d.values()) > K:
        result.append(d)
 
# printing result
print("Dictionaries with summation greater than K : " + str(result))


Output

The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}]
Dictionaries with summation greater than K : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 3, 'is': 7, 'best': 6}]

Time complexity: O(nm), where n is the number of dictionaries in the test_list and m is the average number of key-value pairs in each dictionary. The Auxiliary space: O(k), where k is the number of dictionaries that meet the condition.

METHOD 5:Using re method.

APPROACH:

In this program, we have a list of dictionaries and we need to extract dictionaries from this list whose sum of values is greater than a given value K. We are using regular expressions to extract the values from each dictionary and summing them up. We are then comparing the sum with the given value K to check if it is greater or not. If the sum is greater, we are adding that dictionary to a new list.

ALGORITHM:

1.Initialize an empty list sum_greater_than_k to store the dictionaries whose sum of values is greater than K.
2.Define a regular expression pattern to extract digits from a string.
3.Iterate through the original list of dictionaries and for each dictionary:
a. Convert the dictionary to a string using str() function.
b. Find all the digits from the string using the regular expression pattern.
c. Convert each digit from a string to an integer using map() function and int() function.
d. Find the sum of all the integers using sum() function.
e. Check if the sum is greater than K, if yes, then append the dictionary to the sum_greater_than_k list.
4.Print the original list and the list of dictionaries with summation greater than K.

Python3




import re
 
original_list = [{'Gfg': 4, 'is': 8, 'best': 9},
                 {'Gfg': 5, 'is': 8, 'best': 1},
                 {'Gfg': 3, 'is': 7, 'best': 6},
                 {'Gfg': 3, 'is': 7, 'best': 5}]
k = 14
 
# Extracting dictionaries with values sum greater than K using regex
pattern = re.compile(r'\d+')
sum_greater_than_k = [d for d in original_list if sum(map(int, pattern.findall(str(d)))) > k]
 
# Printing output
print("Original List:", original_list)
print(f"Dictionaries with summation greater than {k}:", sum_greater_than_k)


Output

Original List: [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}]
Dictionaries with summation greater than 14: [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}]

Time Complexity: The time complexity of this program depends on the length of the original list and the number of keys in each dictionary. The regular expression pattern matching and string manipulation takes O(n) time where n is the length of the string. The map() function and sum() function takes O(k) time where k is the number of keys in each dictionary. Therefore, the time complexity of the program will be O(n*k).

Space Complexity: The space complexity of this program depends on the length of the original list and the number of keys in each dictionary. We are storing the dictionaries whose sum of values is greater than K in a new list, so the space complexity will be O(m*k) where m is the number of dictionaries whose sum of values is greater than K and k is the number of keys in each dictionary.



Last Updated : 11 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads