You were close, try the snippet below:

>>> my_dict = {
...     "keyA": 1,
...     "keyB": 2,
...     "keyC": 3
... }
>>> invalid = {"keyA", "keyB"}
>>> def without_keys(d, keys):
...     return {x: d[x] for x in d if x not in keys}
>>> without_keys(my_dict, invalid)
{'keyC': 3}

Basically, the if k not in keys will go at the end of the dict comprehension in the above case.

Answer from Anshul Goyal on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › is there a way to exclude dictionary keys inline when declaring the dictionary?
r/learnpython on Reddit: Is there a way to exclude dictionary keys inline when declaring the dictionary?
August 8, 2022 -

Hi All

The title is a bit of a mess so Ill explain, heres a dictionary

asdf = {
    "a": 1,
    "b": 2,
    "c": 3,
}

but lets say I want the "b" key to be conditionally added based on some other variable foo

If I do this

asdf = {
    "a": 1,
    "b": 2 if foo else None,
    "c": 3,
}

If foo is False, then the output is

{
    "a": 1,
    "b": None,
    "c": 3,
}

But I want to exclude the key and value and get this output

{
    "a": 1,
    "c": 3,
}

Is there any way of doing this inline and not having to something like this

asdf = {
    "a": 1,
    "c": 3,
}

if foo:
    asdf["b"] = 2
Discussions

python - How to exclude a specific element in a dictionary? - Stack Overflow
I want to work with a random name from my_dict , except for the name "name300000.&**" , I want to skip it and pass to another name when choice(my_dict.values()) = "name300000.&am... More on stackoverflow.com
🌐 stackoverflow.com
Copy a dictionary, except some keys - Ideas - Discussions on Python.org
How common is the need of getting a copy of a dict, except some of its keys? It happens to me quite frequently that I need a similar dict to what I have, but without some of its keys. Of course I can’t just remove the keys, as other parts of the code still uses the full dict. More on discuss.python.org
🌐 discuss.python.org
1
October 31, 2019
python - Exclude specific keys of dict when passing to variable - Stack Overflow
im trying to exclude some key when passing a dict to a variable. for example. dict1 = { 'id': 1, 'name' : 'John Woe', 'flag' : True, 'position' : 'Developer' } Now i would like to More on stackoverflow.com
🌐 stackoverflow.com
April 26, 2017
Retain all entries except for one key python - Stack Overflow
I have a python dictionary. Just to give out context, I am trying to write my own simple cross validation unit. So basically what I want is to get all the values except for the given keys. And dep... More on stackoverflow.com
🌐 stackoverflow.com
April 1, 2012
🌐
Python.org
discuss.python.org › ideas
Allow to subtract sets from dicts to exclude keys - Ideas - Discussions on Python.org
October 18, 2024 - Not sure if someone had that idea already. It’s very simple: dct = {"foo": 1, "bar": 2} print(dct - {"foo"}) # prints {"bar": 2} To achieve the equivalent currently, we could use a dict comprehension (I am using an inline set as the ‘excluded keys’ container intentionally in this example, even though it’s not the most optimal): print({k: v for k, v in dct.items() if k not in {"foo"}}) Or a multiline solution (again, using inline set which compiles to a frozenset): dct2 = dct.copy() for ke...
🌐
Python.org
discuss.python.org › ideas
Copy a dictionary, except some keys - Ideas - Discussions on Python.org
October 31, 2019 - How common is the need of getting a copy of a dict, except some of its keys? It happens to me quite frequently that I need a similar dict to what I have, but without some of its keys. Of course I can’t just remove the k…
🌐
Medium
medium.com › @python-javascript-php-html-css › efficiently-removing-keys-from-python-dictionaries-5edc9a4ab9b3
Remove Keys from Python Dictionaries Effectively
August 24, 2024 - These methods provide additional flexibility and can be tailored to suit specific needs when working with Python dictionaries. ... You can use the dictionary.pop(key, None) method to remove a key without raising a KeyError. What is the advantage of using try: except KeyError:? This method ensures that the program continues to run smoothly even if the key does not exist in the dictionary. How does dictionary comprehension work for key removal? Dictionary comprehension creates a new dictionary excluding the specified key using the syntax {k: v for k, v in dictionary.items() if k != key}.
🌐
Python Guides
pythonguides.com › copy-a-python-dictionary-without-some-of-its-keys
How to Copy a Python Dictionary Without One Key
September 10, 2025 - I recommend this method if you’ll be excluding keys often. It keeps your code DRY (Don’t Repeat Yourself) and makes it easy to maintain. If you want a one-liner → use dictionary comprehension. If you want explicit removal → use copy() + pop(). If you’re sure the key exists → use del on a copy. If you need reusability → write a helper function. Copying a Python dictionary without one key is a common task, especially when working with large datasets or cleaning up JSON responses from APIs.
🌐
Finxter
blog.finxter.com › python-print-dictionary-without-one-key-or-multiple-keys
Python Print Dictionary Without One or Multiple Key(s) – Be on the Right Side of Change
October 9, 2022 - The most Pythonic way to print a dictionary except for one or multiple keys is to filter it using dictionary comprehension and pass the filtered dictionary into the print() function.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python-ways-to-remove-a-key-from-dictionary
Python - Ways to remove a key from dictionary - GeeksforGeeks
January 30, 2025 - Explanation: This method iterates through the dictionary and excludes the specified key and creates a new dictionary making it slightly less efficient but useful when immutability is needed.
🌐
Stack Abuse
stackabuse.com › python-how-to-remove-a-key-from-a-dictionary
Python: How to Remove a Key from a Dictionary
September 19, 2021 - In Python, it's commonly known that we can use list comprehensions to create a new list based on an existing one. We can do the same with dictionaries using dict comprehensions. So instead of deleting values in a list, we can use a dict comprehension to create a new dictionary with a condition that excludes the values we don't want.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Remove an Item from a Dictionary in Python: pop, popitem, clear, del | note.nkmk.me
April 27, 2025 - In Python, you can remove an item (key-value pair) from a dictionary (dict) using the pop(), popitem(), clear() methods, or the del statement. You can also remove items based on specific conditions us ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-multiple-keys-from-dictionary
Python - Remove Multiple Keys from Dictionary - GeeksforGeeks
July 12, 2025 - filter() function combined with a lambda function can create a new dictionary excluding the specified keys.
🌐
GitHub
gist.github.com › innat › c8d04ef139060b9ef0b7fa8a164f87bc
Removing Key from Python Dictionary. · GitHub
In addition, I observed that it returned a dictionary which was not ordered the same as myDict. myDict = {'one': 100, 'two': 200, 'three': 300, 'four': 400, 'five': 500} {key:value for key, value in myDict.items() if key != 'one'} If we run it in the shell, it'll execute something like {'five': 500, 'four': 400, 'three': 300, 'two': 200} - notice that it's not the same ordered as myDict. Again if we try to print myDict, then we can see all keys including which we excluded from the dictionary by this approach.
🌐
W3Schools
w3schools.com › python › python_dictionaries_remove.asp
Python - Remove Dictionary Items
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else
🌐
Blue Book
lyz-code.github.io › blue-book › coding › python › pydantic_exporting
Pydantic exporting models - The Blue Book
The dict, json, and copy methods support include and exclude arguments which can either be sets or dictionaries.
🌐
Better Stack
betterstack.com › community › questions › how-to-remove-key-from-python-dictionary
How to remove a key from a Python dictionary? | Better Stack Community
January 26, 2023 - my_dict = {'a': 1, 'b': 2, 'c': 3} value = my_dict.pop('b', 'Key not found') print(value) # Output: 2 print(my_dict) # Output: {'a': 1, 'c': 3} ... This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. ... There are a few ways to make a time delay in Python.