Remove a word from the string if it is used as a key in a dictionary.

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
  1. Take a string and a dictionary as input.
  2. Create an empty string.
  3. Split the input string using split().
  4. Use for loop to iterate over keys of the dictionary.
  5. If the word from the split string is not used as a key in the dictionary, then add that word to the empty dictionary.
  6. 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 
				
			

replace words in a string using a dictionary.

remove duplicate values from dictionary values.

Leave a Comment