Open In App

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. 






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

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]

Time complexity: O(n), where n is the number of dictionaries in the list test_list. This is because the program loops through each dictionary in the list exactly once.
Auxiliary space: O(1), as the program only creates a few variables to store the input and output values, and the space requirements for these variables do not increase with the size of the input. The only variable created with a size proportional to the input is the list res, but its size is bounded by the number of matching values found, which is at most the number of dictionaries in the input list.

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. 




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

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 3: Using the filter() function and lambda function:




# initializing list
test_list = [{'gfg' : 5, 'is' : 8, 'best' : 12},
             {'gfg' : 7, 'is' : 12, 'best' : 24},
             {'gfg' : 20, 'is' : 17, 'best' : 18}]
 
# initializing required key
req_key = 'gfg'
 
# initializing filter key
fil_key = 'best'
 
# initializing filter val
fil_val = 24
 
# using filter and lambda function
res = list(filter(lambda x: x[fil_key] == fil_val, test_list))
res = [d[req_key] for d in res]
 
# printing result
print("The required value : " + str(res))

Output
The required value : [7]

Method #4: Using list comprehension with conditional




# Python3 code to demonstrate working of
# Filter key's value from other key
# Using list comprehension with conditional
 
# 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 with conditional
res = [sub[req_key] for sub in test_list if sub[fil_key] == fil_val]
 
# printing result
print("The required value : " + str(res))

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]

Time Complexity: O(n), where n is the number of sub-dictionaries in the test_list.
Auxiliary Space: O(k), where k is the number of sub-dictionaries in the res list.

Method #6: Using map(), filter(), and lambda function




# initializing list
test_list = [{'gfg' : 5, 'is' : 8, 'best' : 12},
            {'gfg' : 7, 'is' : 12, 'best' : 24},
            {'gfg' : 20, 'is' : 17, 'best' : 18}]
 
# initializing required key
req_key = 'gfg'
 
# initializing filter key
fil_key = 'best'
 
# initializing filter val
fil_val = 24
 
# Using map(), filter(), and lambda function
res = list(map(lambda x: x[req_key], filter(lambda x: x[fil_key] == fil_val, test_list)))
 
# printing result
print("The required value : " + str(res))

Output
The required value : [7]

Time complexity: O(n), where n is the number of dictionaries in the list, 
Auxiliary space: O(n) for the filtered list.

Method # 7: Using a generator expression with multiple conditions




# initializing list
test_list = [{'gfg' : 5, 'is' : 8, 'best' : 12},
            {'gfg' : 7, 'is' : 12, 'best' : 24},
            {'gfg' : 20, 'is' : 17, 'best' : 18}]
 
# initializing required key
req_key = 'gfg'
 
# initializing filter key
fil_key = 'best'
 
# initializing filter val
fil_val = 24
 
# Using a generator expression with multiple conditions
res = list(d[req_key] for d in test_list if d.get(req_key) and d.get(fil_key) == fil_val)
 
# printing result
print("The required value : " + str(res))

Output
The required value : [7]

Time complexity: O(n), where n is the number of dictionaries in test_list.
Auxiliary space: O(k), where k is the number of dictionaries that satisfy the conditions.


Article Tags :