Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Remove Dictionary Key Words

Improve Article
Save Article
  • Last Updated : 16 Mar, 2023
Improve Article
Save Article

Sometimes, while working with Python strings, we can have a problem in which we need to remove all the words from a string which are a part of key of dictionary. This problem can have application in domains such as web development and day-day programming. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using split() + loop + replace() The combination of above functions can be used to solve this problem. In this, we perform the task of converting string to list of words using split(). Then we perform a replace of word present in string with empty string using replace(). 

Python3




# Python3 code to demonstrate working of
# Remove Dictionary Key Words
# Using split() + loop + replace()
 
# initializing string
test_str = 'gfg is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Dictionary
test_dict = {'geeks' : 1, 'best': 6}
 
# Remove Dictionary Key Words
# Using split() + loop + replace()
for key in test_dict:
    if key in test_str.split(' '):
        test_str = test_str.replace(key, "")
 
# printing result
print("The string after replace : " + str(test_str))


Output : 

The original string is : gfg is best for geeks
The string after replace : gfg is  for 

Time Complexity: O(n)

Space Complexity: O(n), where n is length of string.

Method #2 : Using join() + split() This is yet another way in which this task can be performed. In this, we reconstruct new string using join(), performing join by the empty string after split. 

Python3




# Python3 code to demonstrate working of
# Remove Dictionary Key Words
# Using join() + split()
 
# initializing string
test_str = 'gfg is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Dictionary
test_dict = {'geeks' : 1, 'best': 6}
 
# Remove Dictionary Key Words
# Using join() + split()
temp = test_str.split(' ')
temp1 = [word for word in temp if word.lower() not in test_dict]
res = ' '.join(temp1)
 
# printing result
print("The string after replace : " + str(res))


Output : 

The original string is : gfg is best for geeks
The string after replace : gfg is  for 

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3: Using keys(),split() methods and in operator

Python3




# Python3 code to demonstrate working of
# Remove Dictionary Key Words
# Using join() + split()
 
# initializing string
test_str = 'gfg is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Dictionary
test_dict = {'geeks' : 1, 'best': 6}
 
# Remove Dictionary Key Words
# Using join() + split()
temp = test_str.split(' ')
temp1 = [word for word in temp if word.lower() not in test_dict]
res = ' '.join(temp1)
 
# printing result
print("The string after replace : " + str(res))


Output

The original string is : gfg is best for geeks
The string after replace : gfg is for

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using List Comprehension and Join() Method

In this method, use a list comprehension to iterate over the words in the string and check if each word is not a key in the dictionary. If it is not a key, add it to a new list. Finally, join the new list with spaces to get the modified string.

Python




# Python3 code to demonstrate working of
# Remove Dictionary Key Words
# Using List Comprehension and Join() Method
 
# initializing string
test_str = 'gfg is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Dictionary
test_dict = {'geeks' : 1, 'best': 6}
 
# Remove Dictionary Key Words
# Using List Comprehension and Join() Method
new_list = [word for word in test_str.split() if word not in test_dict.keys()]
test_str = " ".join(new_list)
 
# printing result
print("The string after replace : " + str(test_str))


Output

The original string is : gfg is best for geeks
The string after replace : gfg is for

Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(n), where n is the length of the string (used for creating the new list).


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!