Python | Select dictionary with condition given key greater than k
Last Updated :
27 Apr, 2023
In Python, sometimes we require to get only some of the dictionary required to solve the problem. This problem is quite common in web development that we require to get only the selective dictionary satisfying some given criteria. Let’s discuss certain ways in which this problem can be solved.
Method #1: Using list comprehension
Python3
# Python3 code to demonstrate
# filtering of a list of dictionary
# on basis of condition
# initialising list of dictionary
ini_list = [{'a':1, 'b':3, 'c':7}, {'a':3, 'b':8, 'c':17},
{'a':78, 'b':12, 'c':13}, {'a':2, 'b':2, 'c':2}]
# printing initial list of dictionary
print ("initial_list", str(ini_list))
# code to filter list
# where c is greater than 10
res = [d for d in ini_list if d['c'] > 10]
# printing result
print ("resultant_list", str(res))
Outputinitial_list [{'a': 1, 'b': 3, 'c': 7}, {'a': 3, 'b': 8, 'c': 17}, {'a': 78, 'b': 12, 'c': 13}, {'a': 2, 'b': 2, 'c': 2}]
resultant_list [{'a': 3, 'b': 8, 'c': 17}, {'a': 78, 'b': 12, 'c': 13}]
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.
Method #2: Using lambda and filter
Python3
# Python3 code to demonstrate
# filtering of list of dictionary
# on basis of condition
# initialising list of dictionary
ini_list = [{'a':1, 'b':3, 'c':7}, {'a':3, 'b':8, 'c':17},
{'a':78, 'b':12, 'c':13}, {'a':2, 'b':2, 'c':2}]
# printing initial list of dictionary
print ("initial_list", str(ini_list))
# code to filter list
# where c is less than 10
res = list(filter(lambda x:x["c"] > 10, ini_list ))
# printing result
print ("resultant_list", str(res))
Outputinitial_list [{'a': 1, 'b': 3, 'c': 7}, {'a': 3, 'b': 8, 'c': 17}, {'a': 78, 'b': 12, 'c': 13}, {'a': 2, 'b': 2, 'c': 2}]
resultant_list [{'a': 3, 'b': 8, 'c': 17}, {'a': 78, 'b': 12, 'c': 13}]
Method #3: Using dict comprehension and list comprehension
Python3
# Python3 code to demonstrate
# filtering of list of dictionary
# on basis of condition
# initialising list of dictionary
ini_list = [{'a':1, 'b':3, 'c':7}, {'a':3, 'b':8, 'c':17},
{'a':78, 'b':12, 'c':13}, {'a':2, 'b':2, 'c':10}]
# printing initial list of dictionary
print ("initial_list", str(ini_list))
# code to filter list
# where c is more than 10
res = [{ k:v for (k, v) in i.items()}
for i in ini_list if i.get('c') > 10]
# printing result
print ("resultant_list", str(res))
Outputinitial_list [{'a': 1, 'b': 3, 'c': 7}, {'a': 3, 'b': 8, 'c': 17}, {'a': 78, 'b': 12, 'c': 13}, {'a': 2, 'b': 2, 'c': 10}]
resultant_list [{'a': 3, 'b': 8, 'c': 17}, {'a': 78, 'b': 12, 'c': 13}]
Time Complexity: The time complexity of this program is O(n), where n is the number of dictionaries in ini_list.
Auxiliary Space: The auxiliary space used by this program is O(n), where n is the number of dictionaries in ini_list.
Using a for loop:
Approach:
- Create a new empty dictionary
- Iterate over the key-value pairs in the original dictionary using a for loop
- If the key is greater than k, add the key-value pair to the new dictionary
- Return the new dictionary
Python3
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
k = 'c'
new_d = {}
for key, value in d.items():
if key > k:
new_d[key] = value
print(new_d)
Time complexity: O(n), where n is the number of key-value pairs in the original dictionary
Space complexity: O(n), where n is the number of key-value pairs in the new dictionary
Similar Reads
Python - Extract Dictionary Items with Summation Greater than K Given dictionary with value lists, extract all those items with values summing over K. Input : {"Gfg" : [6, 3, 4], "is" : [8, 10, 12], "Best" : [10, 16, 14], "for" : [7, 4, 3], "geeks" : [1, 2, 3, 4]}, K = 10 Output : {"Gfg" : [6, 3, 4], "is" : [8, 10, 12], "Best" : [10, 16, 14], "for" : [7, 4, 3],
5 min read
Python - Storing Elements Greater than K as Dictionary Sometimes, while working with python lists, we can have a problem in which we need to extract elements greater than K. But sometimes, we don't require to store duplicacy and hence store by key value pair in dictionary. To track of number position of occurrence in dictionary. Method #1 : Using loop T
3 min read
Python - Extract dictionaries with values sum greater than K Given a dictionaries list, extract all the dictionaries whose keys summation exceeds K. Input : test_list = [{"Gfg" : 4, "is" : 8, "best" : 9}, {"Gfg" : 3, "is": 7, "best" : 5}], K = 15 Output : [{'Gfg': 4, 'is': 8, 'best': 9}] Explanation : 4 + 9 + 8 = 21. Greater than K, hence returned. Input : te
8 min read
Remove Keys from Dictionary Starting with K - Python We are given a dictionary we need to remove the Keys which are starting with K. For example we are give a dictionary d = {'Key1': 'value1', 'Key2': 'value2', 'other_Key': 'value3'} so that the output should be {'other_Key': 'value3'}Using Dictionary ComprehensionUsing dictionary comprehension we can
3 min read
Python | Count keys with particular value in dictionary Sometimes, while working with Python dictionaries, we can come across a problem in which we have a particular value, and we need to find frequency if it's occurrence. Let's discuss certain ways in which this problem can be solved. Method #1: Using loop This problem can be solved using naive method o
5 min read