Python - Extract dictionaries with values sum greater than K
Last Updated :
11 May, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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)
OutputOriginal 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.
Similar Reads
Python - Extract Dictionary Items with Summation Greater than K 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],
5 min read
Python - Extract dictionaries with Empty String value in K key Given a List of dictionaries, extract all the dictionaries which have empty strings as values of a particular key. Input : test_list = [{"Gfg" : "4", "is" : "good", "best" : "1"}, {"Gfg" : "9", "is" : "CS", "best" : "10"}], K = "Gfg" Output : [] Explanation : No "Gfg" key is empty. Input : test_list
8 min read
Python | Extract filtered Dictionary Values While working with Python dictionaries, there can be cases in which we are just concerned about getting the filtered values list and donât care about keys. This is yet another essential utility and solution to it should be known and discussed. Letâs perform this task through certain methods. Method
4 min read
Python - Extract Key's value from Mixed Dictionaries List Given a list of dictionaries, with each dictionary having different keys, extract value of key K. Input : test_list = [{"Gfg" : 3, "b" : 7}, {"is" : 5, 'a' : 10}, {"Best" : 9, 'c' : 11}], K = 'b' Output : 7 Explanation : Value of b is 7. Input : test_list = [{"Gfg" : 3, "b" : 7}, {"is" : 5, 'a' : 10
7 min read
Python - Test if Values Sum is Greater than Keys Sum in dictionary Given a Dictionary, check if the summation of values is greater than the keys sum. Input : test_dict = {5:3, 1:3, 10:4, 7:3, 8:1, 9:5} Output : False Explanation : Values sum = 19 < 40, which is key sum, i.e false.Input : test_dict = {5:3, 1:4} Output : True Explanation : Values sum = 7 >
8 min read