Python Practice › Dictionary Programs › Program 29
Remove words that are dictionary keys
Write a Python program to remove words from a string if they are keys in a dictionary.
Input
"sqatools is best for learning python", {"best": 2, "learning": 6}Output
sqatools is for python
Approach
- Keep words that are not keys.
Solution
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
Learn the concept
New to this topic? Read the lesson, then come back.