Open In App

Python | Records with Key’s value greater than K

Improve
Improve
Like Article
Like
Save
Share
Report

The problem of getting the suitable dictionaries that has a atleast value of the corresponding key is quite common when one starts working with dictionary. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using loop This is the brute force method by which this task can be performed. For this, we just use naive check and compare and append the records which have a particular key’s value greater than K. 

Python3




# Python3 code to demonstrate working of
# Records with Key's value greater than K
# Using loop
 
# Initialize list
test_list = [{'gfg' : 2, 'is' : 4, 'best' : 6},
            {'it' : 5, 'is' : 7, 'best' : 8},
            {'CS' : 10, 'is' : 8, 'best' : 10}]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initialize K
K = 6
 
# Using loop
# Records with Key's value greater than K
res = []
for sub in test_list:
    if sub['is'] >= K:
        res.append(sub)
 
# printing result
print("The filtered dictionary records is : " + str(res))


Output

The original list is : [{'gfg': 2, 'is': 4, 'best': 6}, {'it': 5, 'is': 7, 'best': 8}, {'CS': 10, 'is': 8, 'best': 10}]
The filtered dictionary records is : [{'it': 5, 'is': 7, 'best': 8}, {'CS': 10, 'is': 8, 'best': 10}]

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

  Method #2 : Using list() + dictionary comprehension The combination of these methods can also be used to perform this task. This difference is that it’s a one liner and more efficient as list function uses iterator as internal implementation which are quicker than generic methods. 

Python3




# Python3 code to demonstrate working of
# Find dictionary matching value in list
# Using list() + dictionary comprehension
 
# Initialize list
test_list = [{'gfg' : 2, 'is' : 4, 'best' : 6},
            {'it' : 5, 'is' : 7, 'best' : 8},
            {'CS' : 10, 'is' : 8, 'best' : 10}]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initialize K
K = 6
 
# Using list() + dictionary comprehension
# Find dictionary matching value in list
res = list((sub for sub in test_list if sub['is'] >= K))
 
# printing result
print("The filtered dictionary records : " + str(res))


Output

The original list is : [{'gfg': 2, 'is': 4, 'best': 6}, {'it': 5, 'is': 7, 'best': 8}, {'CS': 10, 'is': 8, 'best': 10}]
The filtered dictionary records : [{'it': 5, 'is': 7, 'best': 8}, {'CS': 10, 'is': 8, 'best': 10}]

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

 Method #3 : Using filter and lambda

Python3




# Initialize list
original_list = [{'best': 6, 'gfg': 2, 'is': 4}, {'best': 8, 'it': 5, 'is': 7}, {'best': 10, 'CS': 10, 'is': 8}]
print("The original list is : " + str(original_list))
# Using filter() and lambda function to filter the dictionary
k=7
filtered_list = list(filter(lambda x: x['best'] > k, original_list))
# printing result
print("The filtered dictionary records is : " + str(filtered_list))
 
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : [{'best': 6, 'gfg': 2, 'is': 4}, {'best': 8, 'it': 5, 'is': 7}, {'best': 10, 'CS': 10, 'is': 8}]
The filtered dictionary records is : [{'best': 8, 'it': 5, 'is': 7}, {'best': 10, 'CS': 10, 'is': 8}]

#The above code is using filter function to filter the list of dictionaries, where lambda function is used to check the condition of key ‘best’ with value greater than 6. This will return a filtered list of dictionaries where the key ‘best’ have value greater than 6.
#Then we print the original list and filtered list for comparison.

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

Method#4: Using Recursive method.

Algorithm:

  1. Start the function ‘records_greater_than_K’ by taking two inputs, a list of dictionaries ‘test_list’ and an integer ‘K’.
  2. Check the base case if the ‘test_list’ is empty, return an empty list.
  3. If the ‘test_list’ is not empty, check if the first dictionary’s ‘is’ key value is greater than or equal to the integer ‘K’.
  4. If the condition in step 3 is true, add that dictionary to the result list by using list concatenation and call the function recursively with the rest of the list and same ‘K’.
  5. If the condition in step 3 is false, call the function recursively with the rest of the list and same ‘K’.
  6. When there are no more elements in the ‘test_list’, return the result list.
  7. Initialize the ‘test_list’ and ‘K’ variables.
  8. Call the function ‘records_greater_than_K’ with the initialized ‘test_list’ and ‘K’ variables and store the returned result list in ‘res’.
  9. Print the original list and the filtered dictionary records list.

Python3




def records_greater_than_K(test_list, K):
    if not test_list:
        return []
    elif test_list[0]['is'] >= K:
        return [test_list[0]] + records_greater_than_K(test_list[1:], K)
    else:
        return records_greater_than_K(test_list[1:], K)
 
# Initialize list
test_list = [{'gfg' : 2, 'is' : 4, 'best' : 6},
            {'it' : 5, 'is' : 7, 'best' : 8},
            {'CS' : 10, 'is' : 8, 'best' : 10}]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initialize K
K = 6
 
res = records_greater_than_K(test_list, K)
 
 
# printing result
print("The filtered dictionary records : " + str(res))
#this code contributed by tvsk


Output

The original list is : [{'gfg': 2, 'is': 4, 'best': 6}, {'it': 5, 'is': 7, 'best': 8}, {'CS': 10, 'is': 8, 'best': 10}]
The filtered dictionary records : [{'it': 5, 'is': 7, 'best': 8}, {'CS': 10, 'is': 8, 'best': 10}]

The time complexity of this function is O(n), where n is the length of the input list. This is because the function iterates through each element in the list once, so the running time is proportional to the size of the input.

The space complexity of this function is also O(n), because the output list could potentially contain all n elements from the input list if they all satisfy the condition of having a value greater than or equal to K. In the worst case, the output list will be the same size as the input list.

Method #6: Using map() and filter()

This approach uses the map() function to convert each dictionary in the list to a dict object, and then uses filter() to filter out the dictionaries where the value of the ‘is’ key is less than K. The resulting filtered objects are then converted back to dictionaries using the dict() constructor and returned as a list.

  • Initialize the input list of dictionaries test_list.
  • Set the value of K to the threshold value for filtering the dictionaries in the list.
  • Use the map() function to apply the dict() constructor to each dictionary in the test_list and convert it to a dict object. The resulting objects are combined into a map object.
  • Use the filter() function to filter the dictionaries in the map object based on the condition that the value of the ‘is’ key is greater than or equal to K.
  • Convert the resulting filter object back to a list of dictionaries using the list() constructor.
  • The resulting filtered list of dictionaries is the required output.

Python3




# Python3 code to demonstrate working of
# Records with Key's value greater than K
# Using map() and filter()
 
# Initialize list
test_list = [{'gfg' : 2, 'is' : 4, 'best' : 6},
            {'it' : 5, 'is' : 7, 'best' : 8},
            {'CS' : 10, 'is' : 8, 'best' : 10}]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initialize K
K = 6
 
# Using map() and filter()
# Records with Key's value greater than K
res = list(filter(lambda sub: sub['is'] >= K, map(dict, test_list)))
 
# printing result
print("The filtered dictionary records is : " + str(res))


Output

The original list is : [{'gfg': 2, 'is': 4, 'best': 6}, {'it': 5, 'is': 7, 'best': 8}, {'CS': 10, 'is': 8, 'best': 10}]
The filtered dictionary records is : [{'it': 5, 'is': 7, 'best': 8}, {'CS': 10, 'is': 8, 'best': 10}]

Time complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n)



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