Python | Records with Key’s value greater than K

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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}]

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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}]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.