Python - Remove keys with substring values Last Updated : 27 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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 exclude such entries. Let's explore multiple methods to achieve this.Using Dictionary ComprehensionUsing dictionary comprehension is the most efficient method for removing keys with substring values. Python # Example d = {'name1': 'hello world', 'name2': 'python code', 'name3': 'world peace'} substring = 'world' # Remove keys where values contain the substring d = {k: v for k, v in d.items() if substring not in v} print(d) Output{'name2': 'python code'} Explanation:d.items() generates key-value pairs.The condition substring not in v filters out keys with values containing the substring.A new dictionary is created with the remaining pairs.Let's explores some more ways to remove keys with substring values in Python dictionaries.Table of ContentUsing del() with for LoopUsing filter() Using a Temporary DictionaryUsing del() with for LoopThis method uses for loop to delete keys from the dictionary while iterating over its items. Python # Example d = {'name1': 'hello world', 'name2': 'python code', 'name3': 'world peace'} substring = 'world' # Collect keys to remove keys_to_remove = [k for k, v in d.items() if substring in v] # Remove the keys for k in keys_to_remove: del d[k] print(d) Output{'name2': 'python code'} Explanation:A list comprehension collects keys where the values contain the substring.A loop iterates over these keys, removing them using the del() statement.Using filter() This method filters out key-value pairs and reconstructs the dictionary. Python # Example d = {'name1': 'hello world', 'name2': 'python code', 'name3': 'world peace'} substring = 'world' # Filter out keys with substring values d = dict(filter(lambda item: substring not in item[1], d.items())) print(d) Output{'name2': 'python code'} Explanation:filter() iterates through the dictionary items.A lambda function ensures only pairs without the substring are retained.The dict() function converts the filtered pairs back to a dictionary.Using a Temporary DictionaryThis method creates a temporary dictionary to store key-value pairs without the substring. Python # Example d = {'name1': 'hello world', 'name2': 'python code', 'name3': 'world peace'} substring = 'world' # Create a temporary dictionary temp = {} for k, v in d.items(): if substring not in v: temp[k] = v d = temp print(d) Output{'name2': 'python code'} Explanation:A temporary dictionary temp is initialized.A loop adds key-value pairs without the substring to temp.The original dictionary is replaced with temp Comment More infoAdvertise with us Next Article Python - Remove keys with substring values manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Practice Tags : python Similar Reads Python - Remove Keys with K value 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 method 3 min read How to Remove a Substring in Python? In Python, removing a substring from a string can be achieved through various methods such as using replace() function, slicing, or regular expressions. Depending on your specific use case, you may want to remove all instances of a substring or just the first occurrence. Letâs explore different ways 2 min read Python Extract Substring Using Regex Python provides a powerful and flexible module called re for working with regular expressions. Regular expressions (regex) are a sequence of characters that define a search pattern, and they can be incredibly useful for extracting substrings from strings. In this article, we'll explore four simple a 2 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 after substring in String Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring includin 3 min read Remove URLs from string in Python A regular expression (regex) is a sequence of characters that defines a search pattern in text. To remove URLs from a string in Python, you can either use regular expressions (regex) or some external libraries like urllib.parse. The re-module in Python is used for working with regular expressions. I 3 min read Python - Test substring order Given two strings, check if substring characters occur in correct order in string. Input : test_str = 'geeksforgeeks', K = 'sees' Output : True Explanation : "s" after that "ee" and then "s" is present in order in string 1. Input : test_str = 'geeksforgeeks', K = 'seef' Output : False Explanation : 4 min read Python - Remove substring list from String Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by 3 min read Python - Remove suffix from string list To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix. This involves checking each string in the list and ensuring it doesn't have the unwanted suffix at the end, resulting in a list with only the desired elements.Using list comprehensionUsing 3 min read Python - Remove String from String List This particular article is indeed a very useful one for Machine Learning enthusiast as it solves a good problem for them. In Machine Learning we generally encounter this issue of getting a particular string in huge amount of data and handling that sometimes becomes a tedious task. Lets discuss certa 4 min read Like