How to Remove Multiple Keys from a Dictionary in Python?

In this tutorial, I will explain how to remove multiple keys from a dictionary in Python. As a developer working on various projects for my clients, I came across a scenario where I needed to remove multiple keys from a dictionary. After researching various methods I found three efficient methods to accomplish this task. I will share my findings which makes it easy to understand.

Remove Multiple Keys from a Dictionary in Python

Let us learn how to remove multiple keys from a dictionary in Python with examples.

Read How to Create a Dictionary in Python Using a For Loop?

1. Use a Loop

One simple approach to removing multiple keys from a dictionary is to use a loop. You can iterate over the keys you want to remove and use the Python pop() method to delete each key-value pair individually. Here’s an example:

# Initial dictionary
employee_salaries = {
    "John": 50000,
    "Emily": 75000,
    "Michael": 60000,
    "Sarah": 80000,
    "David": 55000
}

# Keys to remove
keys_to_remove = ["John", "David"]

# Remove multiple keys using a loop
for key in keys_to_remove:
    employee_salaries.pop(key, None)

print(employee_salaries)

Output:

{'Emily': 75000, 'Michael': 60000, 'Sarah': 80000}

I executed the above example code and added the screenshot.

Remove Multiple Keys from a Dictionary in Python

In this example, we have a dictionary called employee_salaries that stores employee names and their corresponding salaries. We want to remove the keys “John” and “David” from the dictionary. By iterating over the keys_to_remove list and using the pop() method, we can safely remove each key-value pair. The second argument None is provided to pop() to avoid raising a KeyError if the key doesn’t exist in the dictionary.

Check out Python Append Dictionary

2. Use a Comprehension

Python’s dictionary comprehensions provide a concise way to create new dictionaries based on existing ones. We can leverage this feature to remove multiple keys from a dictionary in a single line of code. Here’s an example:

# Initial dictionary
city_populations = {
    "New York": 8336817,
    "Los Angeles": 3979576,
    "Chicago": 2693976,
    "Houston": 2320268,
    "Phoenix": 1680992
}

# Keys to remove
keys_to_remove = ["Chicago", "Phoenix"]

# Remove multiple keys using a comprehension
city_populations = {key: value for key, value in city_populations.items() if key not in keys_to_remove}

print(city_populations)

Output:

{'New York': 8336817, 'Los Angeles': 3979576, 'Houston': 2320268}

I executed the above example code and added the screenshot.

How to Remove Multiple Keys from a Dictionary in Python

In this example, we have a dictionary city_populations that contains the names of cities and their corresponding populations. We want to remove the keys “Chicago” and “Phoenix” from the dictionary. By using dictionary comprehension, we create a new dictionary that includes only the key-value pairs whose keys are not present in the keys_to_remove list.

Read Python Extend Dictionary

3. Remove Keys Based on a Condition

Sometimes, you may need to remove keys from a dictionary based on a specific condition. For example, let’s say you want to remove all keys that start with a particular letter. You can achieve this by combining the startswith() method with the pop() method in a loop. Here’s an example:

# Initial dictionary
student_grades = {
    "Alice": 85,
    "Bob": 92,
    "Charlie": 78,
    "Diana": 90,
    "Eva": 88
}

# Remove keys starting with 'C' or 'D'
for key in list(student_grades.keys()):
    if key.startswith(('C', 'D')):
        student_grades.pop(key)

print(student_grades)

Output:

{'Alice': 85, 'Bob': 92, 'Eva': 88}

I executed the above example code and added the screenshot.

Remove Multiple Keys from a Dictionary in Python on condition

In this example, we have a dictionary student_grades that stores student names and their corresponding grades. We want to remove all keys that start with the letter ‘C’ or ‘D’. By iterating over a list of the dictionary’s keys and using the startswith() method, we can check if each key starts with the specified letters. If the condition is met, we use the pop() method to remove the corresponding key-value pair from the dictionary.

It’s important to note that we create a new list of keys using list(student_grades.keys()) to avoid modifying the dictionary while iterating over it directly, which can lead to unexpected behavior.

Read Python Filtering Dictionary

Conclusion

In this article, I helped you to learn how to remove multiple keys from a dictionary in Python. I discussed mainly three methods of using a loop, using a comprehension, and removing keys based on a condition.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.