How about a dict comprehension:

filtered_dict = {k: v for k, v in d.iteritems() if filter_string in k}

One you see it, it should be self-explanatory, as it reads like English pretty well.

This syntax requires Python 2.7 or greater.

In Python 3, there is only dict.items(), not iteritems() so you would use:

filtered_dict = {k: v for k, v in d.items() if filter_string in k}
Answer from Jonathon Reinhart on Stack Overflow
Discussions

How to filter a Python dictionary by keys containing a specific substring? - Ask a Question - TestMu AI Community
How can I filter items in a Python dictionary where the keys contain a specific substring? I’m a C programmer transitioning to Python, and I’m familiar with how this can be done in C. In C, I would iterate over the dictionary (or hash map) and check if the key contains a specific substring. More on community.testmuai.com
🌐 community.testmuai.com
0
January 2, 2025
How to filter dictionary keys by substring in Python? - Ask a Question - TestMu AI (formerly LambdaTest) Community
How can I filter items in a Python dictionary where the keys contain a specific substring? I’m a C programmer transitioning to Python, and I’m familiar with how this can be done in C. In C, I would iterate over the dictionary (or hash map) and check if the key contains a specific substring. More on community.testmuai.com
🌐 community.testmuai.com
0
December 25, 2024
python filter list of dictionaries based on key value - Stack Overflow
I have a list of dictionaries and each dictionary has a key of (let's say) 'type' which can have values of 'type1', 'type2', etc. My goal is to filter out these dictionaries into a list of the same More on stackoverflow.com
🌐 stackoverflow.com
Python - Filter list of dictionaries based on if key value contains all items in another list - Stack Overflow
It checks if the filter is a key in the dictionary entries' "abc" value. Your problem was that you used the wrong list comprehension syntax. N.B. You may want to note that you might not be sure that an element has a "abc" key! Thank you for reading this. ... This is an elegant way to do it, I hope it will be useful. ... Find the answer to your question by ... More on stackoverflow.com
🌐 stackoverflow.com
May 23, 2017
🌐
LearnPython.com
learnpython.com › blog › filter-dictionary-in-python
How to Filter a Python Dictionary | LearnPython.com
December 26, 2022 - In the example above, the grades dictionary contains keys (the student’s names) associated with values (each student’s corresponding grade). We can use a particular key, like 'John', to access its corresponding value (7.8 in this case). That’s just a quick recap of Python dictionaries. If you want to learn more about them, we have articles on how to use Python dictionaries and on five ways to create them, so be sure to check them out. Now, let’s move on to filtering ...
🌐
AskPython
askpython.com › python › dictionary › filter-dictionary-string-values
Filter Dictionary Using String Values in Python - AskPython
May 23, 2023 - Here, we need to provide a list of the elements which contain values from the dictionary. Based on the conditions provided in the list we can filter out the dictionaries. Class = { 'stu1' : 'A', 'stu2' : 'B', 'stu3' : 'A', 'stu4': 'C'} def fun_for_filter(pairs): key,value = pairs filter_value = ['A', 'C'] if value in filter_value: return True else: return False Final_dic = dict(filter(fun_for_filter ,Class.items())) print(Final_dic)
🌐
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 - The condition we used is that the new dictionary should only contain the details of the students whose Marks are greater than 350. So we have set a variable called mr that stores the threshold of marks. Next, an empty list called nlist is created to store the result. A for loop with an iterator variable called d is initialized to run through every item in the dictionary dictn to check if the condition is satisfied. The condition is given by the if statement. The key value which satisfies this condition is appended to the new list with the help of append function.
🌐
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).
Find elsewhere
🌐
TestMu AI Community
community.testmuai.com › ask a question
How to filter a Python dictionary by keys containing a specific substring? - Ask a Question - TestMu AI Community
January 2, 2025 - How can I filter items in a Python dictionary where the keys contain a specific substring? I’m a C programmer transitioning to Python, and I’m familiar with how this can be done in C. In C, I would iterate over the dictionary (or hash map) and check if the key contains a specific substring.
🌐
YouTube
youtube.com › watch
filter dictionary to contain only certain keys in python 😀 - YouTube
Code in Python to filter dictionary entries whose keys contains an "o".Support this channel, become a member:https://www.youtube.com/channel/UCBGENnRMZ3chHn_...
Published   July 5, 2018
🌐
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 ...
🌐
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 - We built the most-advanced AI for helping developers at the time, but it fell short of the 10× improvement required to break through because the state of the art for ML on code is not good enough. You can see this in Github Copilot, which is built by Github in collaboration with Open AI.
🌐
HackerNoon
hackernoon.com › filtering-dictionary-in-python-3-3eb99f92e6ee
Filtering Dictionary In Python 3
Discover Anything · Hackernoon · Signup · Write · Light-Mode · Classic · Newspaper · Minty · Dark-Mode · Neon Noir
🌐
Python Guides
pythonguides.com › python-dictionary-filter
How to Filter a Dictionary in Python
November 10, 2025 - Learn how to filter a dictionary in Python using conditions, dictionary comprehensions, and lambda functions. Includes clear examples and practical tips.
🌐
TestMu AI
community.testmuai.com › ask a question
How to filter dictionary keys by substring in Python? - Ask a Question - TestMu AI (formerly LambdaTest) Community
December 25, 2024 - How can I filter items in a Python dictionary where the keys contain a specific substring? I’m a C programmer transitioning to Python, and I’m familiar with how this can be done in C. In C, I would iterate over the dictionary (or hash map) and check if the key contains a specific substring.
Top answer
1 of 4
238

You can try a list comp

>>> exampleSet = [{'type':'type1'},{'type':'type2'},{'type':'type2'}, {'type':'type3'}]
>>> keyValList = ['type2','type3']
>>> expectedResult = [d for d in exampleSet if d['type'] in keyValList]
>>> expectedResult
[{'type': 'type2'}, {'type': 'type2'}, {'type': 'type3'}]

Another way is by using filter

>>> list(filter(lambda d: d['type'] in keyValList, exampleSet))
[{'type': 'type2'}, {'type': 'type2'}, {'type': 'type3'}]
2 of 4
43

Trying a few answers from this post, I tested the performance of each answer.

As my initial guess, the list comprehension is way faster, the filter and list method is second and the pandas is third, by far.

defined variables:

import pandas as pd

exampleSet = [{'type': 'type' + str(number)} for number in range(0, 1_000_000)]

keyValList = ['type21', 'type950000']


1st - list comprehension

%%timeit
expectedResult = [d for d in exampleSet if d['type'] in keyValList]

60.7 ms ± 188 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

2nd - filter and list

%%timeit
expectedResult = list(filter(lambda d: d['type'] in keyValList, exampleSet))

94 ms ± 328 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

3rd - pandas

%%timeit
df = pd.DataFrame(exampleSet)
expectedResult = df[df['type'].isin(keyValList)].to_dict('records')

336 ms ± 1.84 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)


On a side note, using pandas to deal with a dict is not a great idea since the pandas.DataFrame is basically a more memory consuming dict and if you are not going to use a dataframe in the end it is just inefficient.

🌐
GeeksforGeeks
geeksforgeeks.org › python-filter-dictionary-key-based-on-the-values-in-selective-list
Filter Dictionary Key based on the Values in Selective List - Python - GeeksforGeeks
January 28, 2025 - res[key] = val adds the key-value pair to the result dictionary res if the value is in the list. This method leverages the power of NumPy for efficient filtering. By converting the dictionary values to a NumPy array we can utilize vectorized ...
🌐
GitHub
gist.github.com › 89465127 › 5776892
python filter a dictionary by keys or values · GitHub
python filter a dictionary by keys or values · Raw · filter_dict.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
pythontutorials
pythontutorials.net › blog › filter-items-in-a-python-dictionary-where-keys-contain-a-specific-string
How to Filter Python Dictionary Items Where Keys Contain a Specific Substring: The Pythonic Approach — pythontutorials.net
Python’s dict comprehensions are concise, readable, and idiomatic—they embody the "one obvious way to do it" philosophy. The core idea is to iterate over the original dictionary’s items (key-value pairs) and include a pair in the new dictionary only if the key contains the target substring. filtered_dict = {key: value for key, value in original_dict.items() if substring in key}