Python - Remove keys with substring values Last Updated : 12 Jul, 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 M manjeet_04 Follow Improve M manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like