Check if a string contains a dictionary Key in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Check if a string contains a dictionary Key in Python #

To check if a string contains a dictionary key:

  1. Use a for loop to iterate over the dictionary's items.
  2. Check if each dictionary key is contained in the string.
  3. Optionally, append the matching values to a list.
main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com' } my_str = 'website abc 123' values = [] for key, value in my_dict.items(): if key in my_str: # 👇️ site bobbyhadz.com print(key, value) values.append(value) print(values) # 👉️ ['bobbyhadz.com']

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

main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com' } # 👇️ dict_items([('first_name', 'Bobby'), ('last_name', 'Hadz'), ('site', 'bobbyhadz.com')]) print(my_dict.items())

On each iteration, we use the in operator to check if the current key is contained in the string.

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 perform a case-insensitive membership test, convert the key and the string to lowercase.
main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com' } my_str = 'WEBSITE ABC 123' values = [] for key, value in my_dict.items(): if key.lower() in my_str.lower(): # 👇️ site bobbyhadz.com print(key, value) values.append(value) print(values) # 👉️ ['bobbyhadz.com']

The list.append() method adds an item to the end of the list.

If you only need the first value of a key that is contained in the string, use the next() function.

main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com' } my_str = 'website abc 123' first_value = next( (value for key, value in my_dict.items() if key in my_str), None) print(first_value) # 👉️ 'bobbbyhadz.com'

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.

You can also use a list comprehension if you need to get a list of the values whose keys contain the given string.

main.py
my_dict = { 'first_name': 'Bobby', 'last_name': 'Hadz', 'site': 'bobbyhadz.com' } my_str = 'website abc 123' values = [value for key, value in my_dict.items() if key in my_str] print(values) # 👉️ ['bobbyhadz.com']
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 current key is contained in the string.

If the key is contained in the string, the corresponding value is returned.

The new list contains the values of the keys that are contained in the 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.