Remove Item in Dictionary based on Value
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Python dictionaries are optimized for key-based operations, not value-based deletion. So if you want to remove entries by value, the usual solution is either to build a new dictionary with only the values you want to keep or to collect matching keys first and delete them safely afterward.
Remove All Entries with a Matching Value
The cleanest solution is often a dictionary comprehension.
This creates a new dictionary without the unwanted values. It is clear, safe, and usually the best default.
Use this when:
- you want to remove all matching values
- creating a new dictionary is acceptable
- readability matters more than in-place mutation
Remove in Place
If you need to mutate the existing dictionary, do not delete while iterating directly over data.items(). That raises a runtime error because the dictionary size changes during iteration.
Bad pattern:
Safe in-place version:
This works because the list of keys is created before deletion starts.
Remove Only the First Matching Item
Sometimes you want to delete only one entry whose value matches.
This removes the first match according to dictionary iteration order.
In modern Python, dictionaries preserve insertion order, so the notion of "first" is stable in normal code.
Remove by More Complex Value Conditions
You are not limited to equality checks. Any value-based predicate works.
This is useful when the rule is:
- remove empty strings
- remove
None - remove negative numbers
- remove objects that fail some validation test
The comprehension pattern scales well because the filtering logic stays local and explicit.
Nested Structures Need Separate Logic
If the dictionary values are themselves lists, dicts, or other objects, define clearly whether you mean:
- delete the whole key-value pair
- or edit the nested value
Those are different operations.
For example, this removes entries whose nested object is inactive:
Do not confuse value-based dictionary cleanup with mutation of the nested structure itself.
Choose New Dictionary Versus In-Place Mutation Deliberately
In most Python code, building a new dictionary is the simpler and safer option. In-place mutation is useful when:
- the object identity must be preserved
- other code already holds a reference to the same dictionary
- the dictionary is large and you want explicit mutation semantics
Even then, collect keys first before deleting.
Common Pitfalls
- Deleting from a dictionary while iterating over it directly.
- Forgetting that dictionary operations are key-oriented, not value-oriented.
- Using in-place deletion when a simple filtered copy would be clearer.
- Assuming "first matching value" is meaningful without considering insertion order.
- Mixing value-based removal with nested-object mutation logic.
Summary
- The cleanest way to remove items by value is often a dictionary comprehension.
- For in-place deletion, collect keys first and delete afterward.
- Use
next(...)when you only want to remove the first matching entry. - Keep nested-structure filtering logic explicit.
- Choose between new-dictionary creation and in-place mutation based on semantics, not habit.
Related reading
- Remove items from one list in another
- Remove last item from array
- Remove multiple keys from Map in efficient way?
- Remove nodes from graph or reset entire default graph
- Remove Last Two Characters in a String
- remove None value from a list without removing the 0 value
- Removing an activity from the history stack
- Removing an element from an Array Java

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.