In this python dictionary program, we will remove a word from the string if it is a key in a dictionary.
Steps to solve the program
- Take a string and a dictionary as input.
- Create an empty string.
- Split the input string using split().
- Use for loop to iterate over keys of the dictionary.
- If the word from the split string is not used as a key in the dictionary, then add that word to the empty dictionary.
- Print the output.
String = 'sqatools is best for learning python'
Dict = {'best':2,'learning':6}
str2 = ""
for word in String.split(" "):
if word not in Dict:
str2 += word + " "
print(str2)
Output :
sqatools is for python