Open In App

Check If Dictionary Value Contains Certain String with Python

Last Updated : 05 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We need to check if the value associated with a key in a dictionary contains a specific substring. For example, if we have a dictionary of user profiles and we want to check if any user’s description contains a particular word, we can do this easily using various methods. Let’s look at a few ways to check if dictionary values contain a certain string.

Using 'in' operator

This method checks if a substring is present in the value using Python's built-in 'in' operator, which is efficient and simple to use.

Python
a = {'user1': 'loves python', 'user2': 'enjoys reading', 'user3': 'python is fun'}

# Substring to check
b = 'python'

# Checking if the value contains the substring
c = {k: v for k, v in a.items() if b in v}

print(c)

Output
{'user1': 'loves python', 'user3': 'python is fun'}

Explanation:

  • We use a dictionary comprehension to iterate over each key-value pair in the dictionary.
  • The in operator checks if the specified substring is present in the value.
  • If the substring is found, the key-value pair is included in the result.

Let's explore some more ways and see how we can check if dictionary value contains certain string with Python.

Using str.find()

find() method returns the index of the first occurrence of a substring in a string. If the substring is not found, it returns -1. This method can be used to check if a string contains a specific word.

Python
a = {'user1': 'loves python', 'user2': 'enjoys reading', 'user3': 'python is fun'}

# Substring to check
b = 'python'

# Checking if the value contains the substring using find()
c = {k: v for k, v in a.items() if v.find(b) != -1}

print(c)

Output
{'user1': 'loves python', 'user3': 'python is fun'}

Explanation:

  • The find() method is used to search for the substring within the value.
  • If the substring is not found, find() returns -1, so we check if the result is not -1 to include the pair in the result.
  • This method works similarly to the in operator but allows more flexibility if you need to work with the position of the substring.

Using str.__contains__()

This method is essentially the same as the in operator but uses the str.__contains__() special method, which checks if a substring exists in the string.

Python
a = {'user1': 'loves python', 'user2': 'enjoys reading', 'user3': 'python is fun'}

# Substring to check
b = 'python'

# Checking if the value contains the substring using __contains__()
c = {k: v for k, v in a.items() if v.__contains__(b)}

print(c)

Output
{'user1': 'loves python', 'user3': 'python is fun'}

Explanation:

  • The __contains__() method checks if the given substring is found in the string.
  • If the substring is present, it returns True, and the key-value pair is included in the result.
  • This method is more explicit than using the in operator but the in operator is generally preferred due to its simplicity.

Using filter() with lambda function

This method uses the filter() function along with a lambda function to check if the value contains the substring.

Python
a = {'user1': 'loves python', 'user2': 'enjoys reading', 'user3': 'python is fun'}

# Substring to check
b = 'python'

# Using filter() with a lambda to check the condition
c = dict(filter(lambda x: b in x[1], a.items()))

print(c)

Output
{'user1': 'loves python', 'user3': 'python is fun'}

Explanation:

  • filter() function filters out items that do not meet the condition.
  • lambda function checks if the substring is present in the value.

Using regular expressions (re module)

If the substring is more complex or needs to match a pattern, regular expressions can be used. This method allows for advanced string searching capabilities.

Python
import re

a = {'user1': 'loves python', 'user2': 'enjoys reading', 'user3': 'python is fun'}

# Substring to check
b = 'python'

# Using re.search() to check if the value contains the substring
c = {k: v for k, v in a.items() if re.search(b, v)}

print(c)

Output
{'user1': 'loves python', 'user3': 'python is fun'}

Explanation:

  • re.search() method searches for the substring (or pattern) in the string.
  • If the substring is found, re.search() returns a match object, which is considered True in a boolean context.

Next Article
Article Tags :
Practice Tags :

Similar Reads