Constructing a new dict:

dict_you_want = {key: old_dict[key] for key in your_keys}

Uses dictionary comprehension.

If you use a version which lacks them (ie Python 2.6 and earlier), make it dict((key, old_dict[key]) for ...). It's the same, though uglier.

Note that this, unlike jnnnnn's version, has stable performance (depends only on number of your_keys) for old_dicts of any size. Both in terms of speed and memory. Since this is a generator expression, it processes one item at a time, and it doesn't looks through all items of old_dict.

Removing everything in-place:

unwanted = set(old_dict) - set(your_keys)
for unwanted_key in unwanted: del your_dict[unwanted_key]
Answer from user395760 on Stack Overflow
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ filter-dictionary-in-python
How to Filter a Python Dictionary | LearnPython.com
December 26, 2022 - Learn how to filter Python dictionaries by keys and values, including using multiple keys and conditions at once.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to filter a dictionary in python? (โ€ฆ the most pythonic way)
How to Filter a Dictionary in Python? (... The Most Pythonic Way) - Be on the Right Side of Change
October 9, 2022 - However, by reading this short 8-minute tutorial, youโ€™re going to learn a lot about the nuances of writing Pythonic code. So keep reading! You should always start with the simplest method to solve a problem (see Occamโ€™s razor) because premature optimization is the root of all evil! So letโ€™s have a look at the straightforward loop iteration method to filter a dictionary. ... You want to keep those (key, value) pairs where key meets a certain condition (such as key%2 == 1).
Discussions

python - Filter dict to contain only certain keys? - Stack Overflow
I've got a dict that has a whole bunch of entries. I'm only interested in a select few of them. Is there an easy way to prune all the other ones out? More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Filter a dictionary based on a constraint on the keys in a Pythonic way - Stack Overflow
Given a dictionary, I would like to get a new dictionary that is a subset of the original dictionary by checking which keys satisfy a specific constraint. For example, for a dictionary with string ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 6, 2011
filter items in a python dictionary where keys contain a specific string - Stack Overflow
I'm a C coder developing something in python. I know how to do the following in C (and hence in C-like logic applied to python), but I'm wondering what the 'Python' way of doing it is. I have a More on stackoverflow.com
๐ŸŒ stackoverflow.com
Filtering dictionary in python - Stack Overflow
And to get the value of the field 'result', read it from your res dictionary... for k in filterkeys: print(k, res[k]['result']) #with the formatting you need ... Sign up to request clarification or add additional context in comments. ... Find the answer to your question by asking. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-prefix-key-match-in-dictionary
Python | Prefix key match in dictionary - GeeksforGeeks
May 8, 2023 - This is because we are iterating over the items in the dictionary and applying the startswith function to each key, which takes O(m), where m is the length of the key. Auxiliary Space: O(k), where k is the number of items that match the prefix, ...
๐ŸŒ
Online-python
learn.online-python.com โ€บ python-how-to โ€บ filter-dictionaries
Tutorial
# Different ways to filter dictionaries scores = {'Alice': 85, 'Bob': 92, 'Charlie': 78, 'Diana': 95} # Filter by value high_scores = {k: v for k, v in scores.items() if v >= 90} print(f"High scores: {high_scores}") # Filter by key names_with_a = {k: v for k, v in scores.items() if k.startswith('A')} print(f"Names starting with A: {names_with_a}") # Filter by key length short_names = {k: v for k, v in scores.items() if len(k) <= 3} print(f"Short names: {short_names}") # Using filter function def is_high_score(item): return item[1] >= 90 high_scores_filter = dict(filter(is_high_score, scores.items())) print(f"Using filter(): {high_scores_filter}")
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 23407618 โ€บ filtering-dictionary-in-python
Filtering dictionary in python - Stack Overflow
i'm working with dictionaries in python, but can't seem to find a solution. My dictionary looks like this: res = result['result']._asdict() res.keys() ['EGOVMON.CRE.1.2', 'EGOVMON.PDF.03', 'EGOVMON.PDF.05', 'WCAG.PDF.14', 'EGOVMON.TITLE.1.2'] First of all, I want to filter the keys. I only want the keys who starts with 'EGOVMON.PDF' and 'WCAG.PDF'. This is what I have done so far: filterkeys=[v for k,v in res.items() if k.startswith('EGOVMON.PDF') or k.startswith('WCAG.PDF')] print filterkeys will give me a new dictionary: [[<SOAPpy.Types.structType item at 140493364567432>: {'column': 1, 'line': 0, 'type': 'assertion', 'mode': 'automatic', 'result': 1.0}], [<SOAPpy.Types.structType item at 140493353171712>: {'column': 1, 'line': 0, 'type': 'assertion', 'mode': 'automatic', 'result': 0.0}] Now here is where I'm stuck.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ dictionary โ€บ filter-dictionary-string-values
Filter Dictionary Using String Values in Python - AskPython
May 23, 2023 - The dictionary is a data type in Python. The dictionary is mutable, dynamic, and can be nested. The dictionary is always represented in the form of a key-value pair. Dictionaries can be filtered using a built-in method like โ€˜filterโ€™. The filter function will help sort the dictionary according to keys or values.
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-dictionary-filter
How to Filter a Dictionary in Python
November 10, 2025 - The filter() function returns an iterator, so I converted it back to a dictionary using dict(). This is a neat and efficient way to handle filtering when you prefer a functional style in Python. Sometimes you may want to filter a dictionary by specific keys rather than values.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 3283767 โ€บ filter-and-lambda-expressions-with-dictionaries-update-answered
Filter and lambda expressions with dictionaries. Update: Answered | Sololearn: Learn to code for FREE!
July 9, 2024 - The reason I want to know, is because I would like to filter out not just the names that are not 'Klaartje', but al the names that do not start with a 'K'. Normally you would use name[0} != 'K', but now I use the [] for to determine the key/value of the item I am iterating over, so this does not work. I hope one of you can help me. Thankyou in advance! ... Keetie , to get all items from the dict where values start with a `K`, we can just replace a part of the current filter expression: ... item: item[1] == 'Klaartje' ...=> ... item: item[1].startswith('K') ...
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ how-to-filter-a-dictionary-according-to-an-arbitrary-condition-function-in-python.html
How to filter a dictionary according to an arbitrary condition function in python?
Description: Users may search for this query to filter a dictionary based on an arbitrary condition in Python. # Code Implementation: def arbitrary_condition(key, value): return key.startswith('x') or value.endswith('z') my_dict = {'xyz': 'abc', 'xde': 'xyz', 'lmn': 'xyz'} filtered_dict = {k: v for k, v in my_dict.items() if arbitrary_condition(k, v)}
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ filter-list-of-dictionaries-based-on-key-values-in-python
Filter List of Dictionaries Based on Key Values in Python - GeeksforGeeks
July 23, 2025 - We can do this using list comprehensions, the filter() function, and simple loops. List comprehension is one of the most efficient ways to filter a list. It allows us to create a new list by iterating over an existing list and applying a condition.
๐ŸŒ
Kite
kite.com โ€บ python โ€บ answers โ€บ how-to-filter-a-dictionary-by-key-in-python
Kite is saying farewell - Code Faster with Kite
November 20, 2022 - P.S. Most of our code has been open sourced on Github here. It includes our data-driven Python type inference engine, Python public-package analyzer, desktop software, editor integrations, Github crawler and analyzer, and much more.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ dictionary โ€บ filter-list-of-dictionaries-based-on-key-values
3 Ways to Filter List of Dictionaries Based on Key Values - AskPython
April 27, 2023 - To filter a list of dictionaries, we need to have a condition that needs to be satisfied by the keys of the dictionary to create a new filtered dictionary.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-filter-dictionary
Python Filter Dictionary: Unleashing the Power of Data Selection - CodeRivers
February 22, 2026 - For example, if you are accessing keys or values that may not exist, use appropriate error handling mechanisms such as try-except blocks to prevent your program from crashing. Filtering dictionaries in Python is a powerful technique that allows you to extract relevant data based on specific criteria. By using dictionary comprehension, the filter() function, and lambda expressions, you can efficiently filter dictionaries by keys, values, or multiple conditions.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ filter-dictionary-python
Filtering Dictionaries in Python: Concepts, Usage, and Best Practices - CodeRivers
February 22, 2026 - In this loop, we iterate over each key-value pair in the original dictionary. If the value meets the condition, we add the key-value pair to the new dictionary. Filtering by key involves selecting key-value pairs based on the keys in the dictionary.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ python-filter-dictionary-key-based-on-the-values-in-selective-list
Python - Filter dictionary key based on the values in selective list
This approach uses the built-in filter() function combined with dictionary comprehension for a more functional programming style ? # Original dictionary dictA = {'Mon': 'Phy', 'Tue': 'chem', 'Wed': 'Math', 'Thu': 'Bio'} key_list = ['Tue', 'Thu'] print("Given Dictionary:") print(dictA) print("Keys for filter:") print(key_list) # Using filter() function filtered_keys = filter(lambda x: x in dictA, key_list) filtered_dict = {key: dictA[key] for key in filtered_keys} print("Filtered dictionary:") print(filtered_dict)