Python - Remove Keys with K value Last Updated : 28 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a dictionary we need to remove the keys with K value. For example, we are having a dictionary d = {'a': 1, 'b': 2, 'c': 1, 'd': 3} we need to remove the keys which is having K value so that the output should be {'b': 2, 'd': 3} . We can use dictionary comprehension and many other methods to remove keys.Using Dictionary ComprehensionWe can use dictionary comprehension to create a new dictionary excluding the key-value pairs with the specified value. Python d = {'a': 1, 'b': 2, 'c': 1, 'd': 3} K = 1 # Dictionary comprehension to create a new dictionary excluding keys with value equal to K res = {key: value for key, value in d.items() if value != K} print(res) Output{'b': 2, 'd': 3} Explanation:Dictionary Comprehension Iterates through each key-value pair in dictionary d and includes only pairs where the value is not equal to K (which is 1 in this case).Resulting Dictionary new dictionary res contains filtered key-value pairs where the value is not equal to K effectively removing all keys with the value 1Using dict.items()Iterate through dictionary and remove keys with the value K by using the del statement. Python d = {'a': 1, 'b': 2, 'c': 1, 'd': 3} K = 1 # Create a list of keys where the value is equal to K (value to be removed) k = [key for key, value in d.items() if value == K] # Iterate through the list of keys to remove and delete them from the dictionary for key in k: del d[key] print(d) Output{'b': 2, 'd': 3} Explanation:Identify Keys to Remove list comprehension creates a list k containing the keys where value is equal to K (1), which are the keys 'a' and 'c'.Remove Keys from Dictionary for loop iterates through the list k and removes the corresponding key-value pairs from the dictionary d using the del statement.Using filter() and lambdaWe can use the filter() function in combination with a lambda expression to filter out keys with value K. Python d = {'a': 1, 'b': 2, 'c': 1, 'd': 3} K = 1 # Use the filter function to filter out items with value equal to K # The lambda function checks if the value is not equal to K res = dict(filter(lambda item: item[1] != K, d.items())) print(res) Output{'b': 2, 'd': 3} Explanation:filter() Function filters the items in the dictionary d by applying the lambda function, which checks whether the value is not equal to K (1).dict() Conversion filter() function returns an iterable so dict() is used to convert it back into a dictionary resulting in res containing only key-value pairs where value is not equal to K.Using collections.defaultdictThis approach can be helpful if you're working with defaultdict and need to maintain some default behavior Python from collections import defaultdict # Initial defaultdict with default value as int (0) and some key-value pairs d = defaultdict(int, {'a': 1, 'b': 2, 'c': 1, 'd': 3}) K = 1 # Value to be removed from the dictionary # Dictionary comprehension to create a new dictionary excluding keys with value equal to K res = {key: value for key, value in d.items() if value != K} print(res) Output{'b': 2, 'd': 3} Explanation:Create a defaultdict and Filter Keys defaultdict is created with a default value of 0 and some predefined key-value pairs. Then a dictionary comprehension filters out key-value pairs where the value equals K (which is 1).Resulting Dictionary resulting dictionary res contains only the key-value pairs where the value is not equal to K effectively removing all keys with value 1. Comment More infoAdvertise with us Next Article Remove Keys from Dictionary Starting with K - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Practice Tags : python Similar Reads Python - Remove keys with substring values Sometimes, we need to remove keys whose values contain a specific substring. For example, consider the dictionary d = {'name1': 'hello world', 'name2': 'python code', 'name3': 'world peace'}. If we want to remove keys where the value contains the substring 'world', the resulting dictionary should ex 2 min read Python Remove Set Items Python sets are an efficient way to store unique, unordered items. While adding items to a set is straightforward, removing items also offers a variety of methods. This article will guide you through the different techniques available to remove items from a set in Python.Remove single set item using 3 min read Python | Records with Key's value greater than K The problem of getting the suitable dictionaries that has a atleast value of the corresponding key is quite common when one starts working with dictionary. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop This is the brute force method by which this task can be 7 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 - Remove Dictionaries with Matching Values with K Key Given two dictionary lists, remove all the dictionaries which have similar value of K key from other dictionary list. Input : test_list = [{'Gfg' : 3, "is" : 3, "best" : 9}, {'Gfg' : 8, "is" : 4, "best" : 2}, {'Gfg' : 9, "is" : 2, "best" : 4}, {'Gfg' : 8, "is" : 10, "best" : 3}, {'Gfg' : 7, "is" : 1 5 min read Remove Unwanted Keys Associations - Python We are given a dictionary we need to remove the unwanted keys from the dictionary. For example, a = {'a': 1, 'b': 2, 'c': 3, 'd': 4} is a dictionary we need to remove the unwanted keys from the dictionary so that the output becomes {'a':1, 'c':3} .Using del Statementdel statement is used to remove s 2 min read Like