With list comprehensions:

myDict = [{'first': 'James', 'middle': 'Smith', 'last': 'Joule'}, 
      {'first': 'James', 'middle': 'Johnson', 'last': 'Watt'},
      {'first': 'Christian', 'middle': 'Edward', 'last': 'Doppler'},
      {'first': 'Robert', 'last': 'Antonio'}]

keys = {"middle", "last"}

l = [{k:v for k, v in i.items() if k in keys} for i in myDict]

But you can also use map for this:

myDict = [{'first': 'James', 'middle': 'Smith', 'last': 'Joule'}, 
      {'first': 'James', 'middle': 'Johnson', 'last': 'Watt'},
      {'first': 'Christian', 'middle': 'Edward', 'last': 'Doppler'},
      {'first': 'Robert', 'last': 'Antonio'}]

keys = {"middle", "last"}

l = list(map(lambda x: {k:v for k, v in x.items() if k in keys}, myDict))
print(l)

output:

[{'last': 'Joule', 'middle': 'Smith'}, {'last': 'Watt', 'middle': 'Johnson'}, {'last': 'Doppler', 'middle': 'Edward'}, {'last': 'Antonio'}]
Answer from neverwalkaloner on Stack Overflow
Top answer
1 of 2
18

With list comprehensions:

myDict = [{'first': 'James', 'middle': 'Smith', 'last': 'Joule'}, 
      {'first': 'James', 'middle': 'Johnson', 'last': 'Watt'},
      {'first': 'Christian', 'middle': 'Edward', 'last': 'Doppler'},
      {'first': 'Robert', 'last': 'Antonio'}]

keys = {"middle", "last"}

l = [{k:v for k, v in i.items() if k in keys} for i in myDict]

But you can also use map for this:

myDict = [{'first': 'James', 'middle': 'Smith', 'last': 'Joule'}, 
      {'first': 'James', 'middle': 'Johnson', 'last': 'Watt'},
      {'first': 'Christian', 'middle': 'Edward', 'last': 'Doppler'},
      {'first': 'Robert', 'last': 'Antonio'}]

keys = {"middle", "last"}

l = list(map(lambda x: {k:v for k, v in x.items() if k in keys}, myDict))
print(l)

output:

[{'last': 'Joule', 'middle': 'Smith'}, {'last': 'Watt', 'middle': 'Johnson'}, {'last': 'Doppler', 'middle': 'Edward'}, {'last': 'Antonio'}]
2 of 2
1

Use neverwalkaloner's answer if you are just doing this once. But, if you find yourself manipulating lists of dictionaries often, I've written a free library called PLOD that streamlines much of this.

>>> from PLOD import PLOD
>>> l = PLOD(myDict).dropKey("middle").returnList()
>>> l
[{'last': 'Joule', 'first': 'James'}, {'last': 'Watt', 'first': 'James'}, {'last': 'Doppler', 'first': 'Christian'}, {'last': 'Antonio', 'first': 'Robert'}]
>>> print(PLOD(l).returnString())
[
    {first: 'James'    , last: 'Joule'  },
    {first: 'James'    , last: 'Watt'   },
    {first: 'Christian', last: 'Doppler'},
    {first: 'Robert'   , last: 'Antonio'}
]
>>> 

The library is on PyPi: https://pypi.python.org/pypi/PLOD

To more universally do what you want, I'd need to add a new class method. Perhaps .filterKeys. Perhaps I'll do that in version 1.8. It would then go:

>>> l = PLOD(myDict).filterKeys(['first', 'last']).returnList()

Hmmm...

BTW, the lib supports Python 2.7.x right now. Still working on the 3.5.x release.

🌐
LearnPython.com
learnpython.com › blog › filter-dictionary-in-python
How to Filter a Python Dictionary | LearnPython.com
December 26, 2022 - We can apply the same basic logic in order to filter dictionaries in Python. There are only a few differences from the example in the previous section: Instead of elements in a list, we need to iterate over the key-value pairs of the dictionary. We can do this by using the dictionary dict.items() method.
Discussions

python - Filter a dictionary using one of multiple keys - Stack Overflow
I'm new with Pyhton and would like to filter a dictionary with keys composed by two values. Here my dict: {(0, 'DRYER'): [103.0, 131.0, 9.0, 1.24], (2, 'DRYER'): [106.0, 120.0, 5.0, 1.24], (2, ' More on stackoverflow.com
🌐 stackoverflow.com
Python - Filter dictionary with multiple keys - Stack Overflow
My dictionary "mat_contents" looks like this: {'__globals__': [], '__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Wed Jul 05 14:02:28 2017', '__version__': '1.0', 'traindata': ... More on stackoverflow.com
🌐 stackoverflow.com
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 - Filtering a list of dictionaries based on multiple values - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... New site design and philosophy for Stack Overflow: Starting February 24, 2026... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... 1 Find all keys which match multiple values in dictionary · 9 How to filter ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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. A dictionary is the most popularly used data storage structure of Python.
🌐
AskPython
askpython.com › python › dictionary › filter-dictionary-string-values
Filter Dictionary Using String Values in Python - AskPython
May 23, 2023 - When we need to add multiple conditions, then providing a list of that conditions would be the best option. Here, we will assign the list of ‘keys’ so, these ‘keys’ will be excluded or included in the dictionary. Class = { 'stu1' : 'A', 'stu2' : 'B', 'stu3' : 'A', 'stu4': 'C'} def fun_for_filter(pairs): key,value = pairs filter_key = ['stu1','stu4'] if key in filter_key: return True else: return False Final_dic = dict(filter(fun_for_filter ,Class.items())) print(Final_dic)
🌐
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).
🌐
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.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 45368697 › python-filter-dictionary-with-multiple-keys
Python - Filter dictionary with multiple keys - Stack Overflow
I want to filter (subset) a given number of rows (containing 'traindata' and 'trainlabels') according to a value of trainlabels. So far (with help) I got the following command: labels = [mat_contents['trainlabels'][0][i] for i, val in enumerate(mat_contents['traindata'].flatten()[0]) if val == 1] result = np.random.choice(labels, 2, replace=False) ... I tried to solve in a previous question: Python - Subtract a number of samples from a given in a dictionary structure
🌐
GeeksforGeeks
geeksforgeeks.org › filtering-a-list-of-dictionary-on-multiple-values-in-python
Filtering a List of Dictionary on Multiple Values in Python | GeeksforGeeks
February 7, 2024 - Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the values—specifically whe ... We are given a dictionary where each key is associated with a value and our task is to filter the dictionary based on a selective list of values.
🌐
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 ...
🌐
w3resource
w3resource.com › python-exercises › dictionary › python-data-type-dictionary-exercise-42.php
Python: Filter a dictionary based on values - w3resource
June 28, 2025 - Write a Python program to filter a dictionary and return only those entries where the value exceeds a given threshold. Write a Python program to use a lambda function in dictionary comprehension to keep only key-value pairs satisfying a condition. Write a Python program to create a new dictionary by ...
🌐
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)
🌐
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.
🌐
Mark Needham
markhneedham.com › blog › 2020 › 04 › 27 › python-select-keys-from-map-dictionary
Python: Select keys from map/dictionary | Mark Needham
April 27, 2020 - Or we can iterate over all the entries in the map and filter it that way: >>> {key:value for key,value in x.items() if key in ["a", "b"]} {'a': 1, 'b': 2} This approach is longer but more flexible. For example, we could find the keys and values for all entries with a value great than 2 with ...
🌐
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.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to filter a list of dictionaries in python?
How to Filter a List of Dictionaries in Python? - Be on the Right Side of Change
April 17, 2020 - Here’s the code that shows you how to filter out all user dictionaries that don’t meet the condition of having a key play_time.