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 dictionary by value?
Use dictionary comprehensions. They work pretty much the same as list comprehensions and generator expressions. filtered_dict = {key: value for key, value in old_dict.iteritems() if value > 90} More on reddit.com
๐ŸŒ r/learnpython
10
12
December 10, 2013
How to filter a nested dict by key?
To be honest I am not sure what some of the middle code is for, so I'm sorry if this is missing some functionality that you need. This takes your src_tgt_dict and tgt_preps and outputs the new_src_tgt_dict you're looking for (again sorry if it's missing in-betweens that you need): src_tgt_dict = {"each":{"chaque":3}, "in-front-of":{"devant":4}, "next-to":{"ร -cรดtรฉ-de":5}, "for":{"pour":7}, "cauliflower":{"chou-fleur":4}, "on":{"sur":2, "panda-et":2}} tgt_preps = ["devant", "pour", "sur", "ร "] new_tgt_dict = {} for i in src_tgt_dict.items(): for j in tgt_preps: # i[1].keys() is dict_keys([french word]) # list(i[1].keys())[0] returns the word itself # [:len(j)] checks start of string (do you need hyphenation?) if j in list(i[1].keys())[0][:len(j)]: print(i) new_tgt_dict.update({i[0]: i[1]}) More on reddit.com
๐ŸŒ r/learnpython
6
2
February 6, 2022
Filter Dataframe by Dictionary Values
First, group your data by client using df.groupby('client'). Then use transform on the groupby object to populate each row with the mean of its respective client. Iโ€™d assign the output to a new column. Then use apply to populate an additional column populated with the row-wise outputs of your purchases function. The general outlines of the above should be something like this: df['client_mean'] = df.groupby('client').transform('mean') df['phrase'] = df.apply(purchases, axis=1) However for this to work, youโ€™ll need to modify your purchases function to accept a series (which corresponds to a row from your df). This might look something like the following: def purchases(row): if row.value < row.client_mean: return random.choice(list_of_strings) else: return pd.NA Not sure if that else clause is necessary, youโ€™ll just have to try it and see. More on reddit.com
๐ŸŒ r/learnpython
9
1
May 11, 2021
Delete empty keys in a dictionary?

Those aren't empty keys, they're empty values in a list. You can remove them with a list comprehension:

Python 3.6.0b1 (default, Sep 12 2016, 18:11:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> foods = ["spam", "ham", "eggs", "bacon", "spam", "", "", ""]
>>> [x for x in foods if x]
['spam', 'ham', 'eggs', 'bacon', 'spam']

Also, this seems like a bad use case for CSV. Are you using an existing file with data, or are you building it up itself? If it's the latter, consider JSON or any other non-tabular format, or transpose your table so it looks something like

Alarak true false Artanis true false Lunara false true More on reddit.com
๐ŸŒ r/learnpython
25
14
October 15, 2016
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ filter-list-of-python-dictionaries-by-key-in-python
Filter List of Python Dictionaries by Key in Python - GeeksforGeeks
July 23, 2025 - After applying map(), we filter out None values with a list comprehension. for loop in Python is a more traditional approach to filter dictionaries by key.
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ filter-dictionary-in-python
How to Filter a Python Dictionary | LearnPython.com
December 26, 2022 - Did you notice that we still have not used the value variable inside the filtering function? If we want to filter a Python dictionary by value, we simply need to use this variable at any point in our filtering logic.
๐ŸŒ
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 removing entries whose values do not meet a specified predicate.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-filter-dictionary-values-in-heterogeneous-dictionary
Python - Filter dictionary values in heterogeneous dictionary - GeeksforGeeks
May 9, 2024 - Print the filtered dictionary using print("Values greater than K : " + str(res)). Below is the implementation of the above approach: ... # Python3 code to demonstrate working of # Filter dictionary values in heterogeneous dictionary # Using for loop and conditional statements # initializing dictionary test_dict = {'Gfg' : 4, 'is' : 2, 'best' : 3, 'for' : 'geeks'} # printing original dictionary print("The original dictionary : " + str(test_dict)) # initializing K K = 3 # Filter dictionary values in heterogeneous dictionary # Using for loop and conditional statements res = {} for key, val in test_dict.items(): if type(val) != int or val > K: res[key] = val # printing result print("Values greater than K : " + str(res))
Find elsewhere
๐ŸŒ
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 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. Next, the new list is printed. Lastly, we are converting the list to a dictionary and printing the filtered dictionary.
๐ŸŒ
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.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ standard-library โ€บ dict โ€บ items
Python dict items() - Retrieve Key-Value Pairs | Vultr Docs
November 8, 2024 - This script loops through each key-value pair in my_dict, accessing the dictionary items via the items() method. The print function then displays each key and its corresponding value. Filter dictionary entries based on a condition using the items() method...
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-dictionary-comprehension
Python Dictionary Comprehension Tutorial | DataCamp
December 4, 2024 - In this Python tutorial, you'll learn how to create a dictionary, load data in it, filter, get and sort the values, and perform other dictionary operations.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ dictionary โ€บ filter-dictionary-string-values
Filter Dictionary Using String Values in Python - AskPython
May 23, 2023 - In this way, we can access the value of any key if we define that key with the dictionary name inside the โ€˜[ ]โ€™ brackets. The filter () function in Python is used for filtering the dictionaries.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_dictionaries_access.asp
Python - Access Dictionary Items
The list of the values is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the values list.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-use-list-comprehension-to-filter-keys-in-a-python-dictionary-based-on-their-values-417459
How to use list comprehension to filter keys in a Python dictionary based on their values | LabEx
In this tutorial, we will explore how to leverage Python's list comprehension to filter the keys of a dictionary based on their corresponding values. List comprehension is a concise and powerful way to create new lists in Python, and it can be particularly useful when working with dictionaries. By ...
๐ŸŒ
Medium
medium.com โ€บ @muirujackson โ€บ set-dictionary-and-lambda-filter-reduce-and-map-functions-4b304a30a0da
Set, Dictionary, and Lambda, filter, reduce and map functions | by Muiru Jackson | Medium
June 14, 2023 - In this article, we explored sets and their methods, compared sets with lists, learned how to iterate over sets, discussed dictionaries and their advantages, and delved into lambda functions, map, reduce, and filter functions. These concepts are essential for any Python programmer looking to work with different data structures efficiently and leverage powerful functions to simplify coding tasks. By understanding these topics, you can enhance your Python skills and become more proficient in writing clean and effective code.
๐ŸŒ
Real Python
realpython.com โ€บ iterate-through-dictionary-python
How to Iterate Through a Dictionary in Python โ€“ Real Python
November 23, 2024 - The fastest way to access both keys and values when you iterate over a dictionary in Python is to use .items() with tuple unpacking. To get the most out of this tutorial, you should have a basic understanding of Python dictionaries, know how to use Python for loops, and be familiar with comprehensions. Knowing other tools like the built-in map() and filter() functions, as well as the itertools and collections modules, is also a plus.
๐ŸŒ
Python-Fiddle
python-fiddle.com โ€บ challenges โ€บ filter-dictionary-by-value
Python Challenge: Filter Dictionary by Value
def filter_dict_by_value(data: dict, n: int) -> dict: """ Filters a dictionary to include only entries with values greater than or equal to n. Args: data (dict): The input dictionary with string keys and integer values. n (int): The threshold value. Returns: dict: A dictionary containing only ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.6 documentation
The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgotten. Extracting a value for a non-existent key by subscripting (d[key]) raises a KeyError.
๐ŸŒ
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 - but we can use the membership operator `in`. to get it done we can filter for the letter `a` like: ... k_names = dict(filter(lambda item: 'a' in item[1], figures.items())) > result will be: {'chicken': 'Klaartje', 'horse': 'Karel', 'duck': 'Donald'} >> to make the filtering with the dicts a bit more general, we can use input values, store them in a variable,and use this variable instead of the *hardcoded* samples we have been talking so far. ... if you are looking for more complex patterns than startswith(), endswith(), and "in", consider "regular expressions" (not sure if it is covered in the current courses?). ... Lisa , regex is *not* explained in the current available python tutorials.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_dictionaries.asp
Python Dictionaries
In Python 3.6 and earlier, dictionaries are unordered. Dictionaries are written with curly brackets, and have keys and values: ... Dictionary items are ordered, changeable, and do not allow duplicates. Dictionary items are presented in key:value pairs, and can be referred to by using the key name.