Check if dictionary value contains certain String in Python

avatar

Borislav Hadzhiev

Last updated: Sep 19, 2022

banner

Photo from Unsplash

Check if dictionary value contains certain String in Python #

To check if any value in a dictionary contains a certain string:

  1. Use a for loop to iterate over the dictionary's items.
  2. Use an if statement to check if the string is contained in each value.
main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'language': ['js', 'py', 'ts'] } # ✅ check if any value in a dictionary contains a string for key, value in my_dict.items(): if 'bobbyhadz' in value: # 👇️ names ['alice', 'bobbyhadz', 'carl'] print(key, value) # ------------------------------------------------------- # ✅ check if any value in a dictionary contains a substring for key, value in my_dict.items(): for item in value: if 'hadz' in item: # 👇️ names ['alice', 'bobbyhadz', 'carl'] bobbyhadz print(key, value, item)

The first example checks if any value in a dictionary contains a given string.

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

main.py
my_dict = {'id': 1, 'name': 'BobbyHadz'} print(my_dict.items()) # 👉️ dict_items([('id', 1), ('name', 'BobbyHadz')])

We used a for loop to iterate over the dictionary's items.

On each iteration, we check if the specified string is contained in the value (list) of the current iteration using the in operator.

main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'language': ['js', 'py', 'ts'] } for key, value in my_dict.items(): if 'bobbyhadz' in value: # 👇️ names ['alice', 'bobbyhadz', 'carl'] print(key, value)

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

If you don't need to get the corresponding key, iterate over the dictionary's values.

main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'language': ['js', 'py', 'ts'] } for value in my_dict.values(): if 'bobbyhadz' in value: # 👇️ ['alice', 'bobbyhadz', 'carl'] print(value)

The dict.values method returns a new view of the dictionary's values.

main.py
my_dict = {'id': 1, 'name': 'bobbyhadz'} print(my_dict.values()) # 👉️ dict_values([1, 'bobbyhadz'])

To check if any dictionary value contains a substring:

  1. Use a for loop to iterate over the dictionary's items.
  2. Use a for loop to iterate over each value.
  3. Check if the substring is contained in each item.
main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'language': ['js', 'py', 'ts'] } for key, value in my_dict.items(): for item in value: if 'hadz' in item: # 👇️ names ['alice', 'bobbyhadz', 'carl'] bobbyhadz print(key, value, item)

The first for loop iterates over the dictionary's items.

The second for loop iterates over each value (list) in the dictionary.

On each iteration, we use the in operator to check if the substring is contained in each item.

If the condition is met, we print the corresponding key, value and item.

If you only need to access the matching value, use the dict.values() method instead.

main.py
my_dict = { 'names': ['alice', 'bobbyhadz', 'carl'], 'tasks': ['develop', 'test', 'ship'], 'language': ['js', 'py', 'ts'] } for value in my_dict.values(): for item in value: if 'hadz' in item: # 👇️ ['alice', 'bobbyhadz', 'carl'] bobbyhadz print(value, item)
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.