Remove keys whose values contain a substring

EasyDictionary Programs · 69 of 94

Write a Python program to remove entries whose value contains any of the given substrings.

Advertisement
Input
{1: "sqatools is best", 2: "for learning python"}, ["best", "excellent"]
Output
{2: 'for learning python'}

Approach

  1. Keep values where no substring is present.

Solution

Python
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.

More dictionary programs

← All 94 Dictionary Programs

Leave a Comment