# Python – Filter key’s value from other key

Last Updated : 16 May, 2023

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

 # 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

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

## Python3

 # 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

• Initialize the list comprehension by enclosing it in square brackets.
• Use the for loop to iterate over each sub-dictionary in the test_list.
• Use a conditional statement to check if the value of the fil_key in the current sub-dictionary is equal to fil_val.
• If the condition is true, append the value of the req_key to the list.
• Print the resulting list.

## Python3

 # 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

• Use the map() function with a lambda function to extract the values of the ‘gfg’ key from each dictionary in the list. The lambda function takes a dictionary as input and returns the value of the ‘gfg’ key.
• Use the filter() function with a lambda function to select the dictionaries that have a value of 24 for the ‘best’ key. The lambda function takes a dictionary as input and returns True if the value of the ‘best’ key is 24.
• Use the list() function to convert the map object returned by the map() function to a list.
• Print the list of values of the ‘gfg’ key from the filtered dictionaries.

## Python3

 # 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

• Use a generator expression to iterate over the list of dictionaries test_list.
• For each dictionary d in test_list, use the following condition:
• This condition checks if the dictionary has a key called req_key and the value of the key fil_key is equal to fil_val.
• If the condition is true, yield the value of the key req_key in the dictionary.
• Use the list() function to convert the generator expression to a list and store it in the variable res.
• Print the result.

## Python3

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

Previous
Next