Python Practice › Dictionary Programs › Program 69
Remove keys whose values contain a substring
Write a Python program to remove entries whose value contains any of the given substrings.
Input
{1: "sqatools is best", 2: "for learning python"}, ["best", "excellent"]Output
{2: 'for learning python'}Approach
- Keep values where no substring is present.
Solution
d = {1: "sqatools is best", 2: "for learning python"}subs = ["best", "excellent"]print({k: v for k, v in d.items() if not any(s in v for s in subs)})Output
{2: 'for learning python'}Learn the concept
New to this topic? Read the lesson, then come back.