Python – Filter key’s value from other key

Sometimes, while working with Python dictionary, we can have a problem in which we need to extract a value from dictionary list of the key on basis of some other key equality. This kind of problem is common in domains that include data, for e.g web development. Let’s discuss certain ways in which this task can be performed.

Input :
test_list = [{‘gfg’ : 5, ‘is’ : 8, ‘best’ : 24}, {‘gfg’ : 7, ‘is’ : 12, ‘best’ : 24}]
req_key = ‘gfg’ [ Requested Key ]
fil_key = ‘best’ [ Filtering Key ]
fil_val = 24 [ Filtering value to be checked ]
Output : [5, 7]

Input :
test_list = [{‘gfg’ : 5, ‘is’ : 8, ‘best’ : 24}]
req_key = ‘gfg’ [ Requested Key ]
fil_key = ‘best’ [ Filtering Key ]
fil_val = 24 [ Filtering value to be checked ]
Output : [5]

Method #1 : Using loop
This is brute force way to solve this problem. In this, we manually iterate the entire list and check for filter key’s value, on equality we extract the required key’s value.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Filter key's value from other key
# Using loop
  
# initializing list
test_list = [{'gfg' : 5, 'is' : 8, 'best' : 12}, 
             {'gfg' : 7, 'is' : 12, 'best' : 24},
             {'gfg' : 20, 'is' : 17, 'best' : 18}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing required key
req_key = 'gfg'
  
# initializing filter key
fil_key = 'best'
  
# initializing filter val 
fil_val = 24
  
# Filter key's value from other key
# Using loop
res = []
for sub in test_list:
  if sub[fil_key] == fil_val:
    res.append(sub[req_key])
  
# printing result 
print("The required value : " + str(res)) 

chevron_right


Output :



The original list is : [{‘gfg’: 5, ‘is’: 8, ‘best’: 12}, {‘gfg’: 7, ‘is’: 12, ‘best’: 24}, {‘gfg’: 20, ‘is’: 17, ‘best’: 18}]
The required value : [7]

 

Method #2 : Using list comprehension
This is yet another way to solve this problem. In this, we perform the task similar in above method, in a shorthand way using list comprehension construct.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Filter key's value from other key
# Using list comprehension
  
# initializing list
test_list = [{'gfg' : 5, 'is' : 8, 'best' : 12}, 
             {'gfg' : 7, 'is' : 12, 'best' : 24},
             {'gfg' : 20, 'is' : 17, 'best' : 18}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing required key
req_key = 'gfg'
  
# initializing filter key
fil_key = 'best'
  
# initializing filter val 
fil_val = 24
  
# Filter key's value from other key
# Using list comprehension
res = [sub[req_key] for sub in test_list if sub[fil_key] == fil_val]
  
# printing result 
print("The required value : " + str(res)) 

chevron_right


Output :

The original list is : [{‘gfg’: 5, ‘is’: 8, ‘best’: 12}, {‘gfg’: 7, ‘is’: 12, ‘best’: 24}, {‘gfg’: 20, ‘is’: 17, ‘best’: 18}]
The required value : [7]




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.