Open In App

Python | Extract filtered Dictionary Values

Last Updated : 23 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

While working with Python dictionaries, there can be cases in which we are just concerned about getting the filtered values list and don’t care about keys. This is yet another essential utility and solution to it should be known and discussed. Let’s perform this task through certain methods. 

Method #1 : Using loop + keys() The first method that comes to mind to achieve this task is the use of loop to access each filtered key’s value and append it into a list and return it. This can be one of method to perform this task. 

Python3




# Python3 code to demonstrate working of
# Extract filtered Dictionary Values
# Using loop + keys()
 
# initializing dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 2
 
# Extract filtered Dictionary Values
# Using loop + keys()
res = []
for key in test_dict.keys() :
    if test_dict[key] >= K:
        res.append(test_dict[key])
 
# printing result
print("The list of filtered values is : " + str(res))


Output

The original dictionary is : {'gfg': 1, 'is': 2, 'best': 3}
The list of filtered values is : [2, 3]

  Method #2 : Using values() This task can also be performed using the inbuilt function of values(). This is the best and most Pythonic way to perform this particular task and returns the filtered exact desired result. 

Python3




# Python3 code to demonstrate working of
# Extract filtered Dictionary Values
# Using values()
 
# initializing dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 2
 
# Extract filtered Dictionary Values
# Using values()
temp = list(test_dict.values())
res = [ele for ele in temp if ele >= K]
 
# printing result
print("The list of filtered values is : " + str(res))


Output

The original dictionary is : {'gfg': 1, 'is': 2, 'best': 3}
The list of filtered values is : [2, 3]

Method #3 : Using filter() and lambda
This method uses the filter() function along with a lambda function to filter the values of the dictionary. It is similar to using a list comprehension, but may be more readable to some users.

Python3




# Python3 code to demonstrate working of
# Extract filtered Dictionary Values
# Using filter() and lambda
 
# initializing dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 2
 
# Extract filtered Dictionary Values
# Using filter() and lambda
res = list(filter(lambda x: x >= K, test_dict.values()))
 
# printing result
print("The list of filtered values is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original dictionary is : {'gfg': 1, 'is': 2, 'best': 3}
The list of filtered values is : [2, 3]

Time Complexity: O(n) where n is the number of elements in the dictionary.
Auxiliary Space: O(n) where n is the number of elements in the filtered list.

Method 4 :  using a list comprehension:

Explanation:

  1. We start by initializing the dictionary test_dict and printing its contents.
  2. We initialize the threshold value K to 2.
  3. To extract the filtered dictionary values, we use a list comprehension. Here’s how it works:
  4. We iterate through each key-value pair in the dictionary using the items() method.
  5. For each key-value pair, we check if the value is greater than or equal to K.
  6. If the condition is true, we add the value to a list.
  7. Finally, we return the list of filtered values.
  8. We print the list of filtered values.

Python3




# initializing dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 2
 
# Extract filtered Dictionary Values
res = [value for key, value in test_dict.items() if value >= K]
 
# printing result
print("The list of filtered values is : " + str(res))


Output

The original dictionary is : {'gfg': 1, 'is': 2, 'best': 3}
The list of filtered values is : [2, 3]

Time complexity: O(n) where n is the number of items in the dictionary.

Auxiliary space: O(m) where m is the number of items that satisfy the filtering condition.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads