Find dictionary items whose key matches Substring in Python

avatar

Borislav Hadzhiev

Last updated: Sep 19, 2022

banner

Photo from Unsplash

Find dictionary items whose key matches Substring in Python #

To find a dictionary's items whose key matches a substring:

  1. Use a list comprehension to iterate over the dictionary's items.
  2. Check if the substring is contained in each key.
  3. The new list will only contain the values of the matching keys.
main.py
my_dict = { 'First_Name': 'Bobby', 'Last_Name': 'Hadz', 'language': 'Python' } partial_key = 'name' matching_values = [value for key, value in my_dict.items( ) if partial_key.lower() in key.lower()] print(matching_values) # 👉️ ['Bobby', 'Hadz']

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
my_dict = { 'First_Names': 'Bobby', 'Last_Names': 'Hadz', 'language': 'Python' } # 👇️ dict_items([('First_Name', 'Bobby'), ('Last_Name', 'Hadz'), ('language', 'Python')]) print(my_dict.items())

We used a list comprehension to iterate over the dictionary's items.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the substring is contained in the current key.

main.py
my_dict = { 'First_Name': 'Bobby', 'Last_Name': 'Hadz', 'language': 'Python' } partial_key = 'name' matching_values = [value for key, value in my_dict.items( ) if partial_key.lower() in key.lower()] print(matching_values) # 👉️ ['Bobby', 'Hadz']

If the condition is met, we return the corresponding value.

The new list only contains the values of the matching keys.

We used the str.lower() method to convert the substring and the key to lowercase to test for membership ignoring the case.

Converting both strings to lowercase or uppercase allows for a case-insensitive string comparison.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

If you need to get a dictionary of key-value pairs containing the matching items, use a dict comprehension.

main.py
my_dict = { 'First_Name': 'Bobby', 'Last_Name': 'Hadz', 'language': 'Python' } partial_key = 'name' matching_items = { key: value for key, value in my_dict.items() if partial_key.lower() in key.lower() } # 👇️ {'First_Name': 'Bobby', 'Last_Names': 'Hadz'} print(matching_items)

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary or select a subset of key-value pairs that meet a condition.

The new dictionary only contains the items whose key matches the specified substring.

If you only need the first dictionary value whose key contains the given string, use the next() function.

main.py
my_dict = { 'First_name': 'Bobby', 'Last_name': 'Hadz', 'language': 'Python' } partial_key = 'name' first_match = next( (value for key, value in my_dict.items( ) if partial_key.lower() in key.lower()), None ) print(first_match) # 👉️ Bobby

When we pass the iterator to the next() function, the next item in the stream is returned.

We specified a default value of None which is used if none of the keys in the dictionary contains the given string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.