dictionary
python programming
remove item
value-based deletion
coding tutorial

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.

Practice algorithms

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.

python
1data = {
2    "a": 1,
3    "b": 2,
4    "c": 1,
5    "d": 3,
6}
7
8filtered = {k: v for k, v in data.items() if v != 1}
9print(filtered)  # {'b': 2, 'd': 3}

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:

python
for k, v in data.items():
    if v == 1:
        del data[k]  # unsafe during active iteration

Safe in-place version:

python
1data = {
2    "a": 1,
3    "b": 2,
4    "c": 1,
5    "d": 3,
6}
7
8keys_to_remove = [k for k, v in data.items() if v == 1]
9
10for key in keys_to_remove:
11    del data[key]
12
13print(data)  # {'b': 2, 'd': 3}

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.

python
1data = {
2    "a": 1,
3    "b": 2,
4    "c": 1,
5}
6
7key_to_remove = next((k for k, v in data.items() if v == 1), None)
8
9if key_to_remove is not None:
10    del data[key_to_remove]
11
12print(data)

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.

python
1data = {
2    "a": 10,
3    "b": 3,
4    "c": 7,
5    "d": 1,
6}
7
8filtered = {k: v for k, v in data.items() if v >= 5}
9print(filtered)  # {'a': 10, 'c': 7}

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:

python
1data = {
2    "u1": {"active": True},
3    "u2": {"active": False},
4}
5
6filtered = {k: v for k, v in data.items() if v["active"]}
7print(filtered)

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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.