Remove words that are dictionary keys

EasyDictionary Programs · 29 of 94

Write a Python program to remove words from a string if they are keys in a dictionary.

Advertisement
Input
"sqatools is best for learning python", {"best": 2, "learning": 6}
Output
sqatools is for python

Approach

  1. Keep words that are not keys.

Solution

Python
text = "sqatools is best for learning python"d = {"best": 2, "learning": 6}print(" ".join(w for w in text.split() if w not in d))
Output
sqatools is for python
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs