Open In App

Python – Replace words from Dictionary

Last Updated : 03 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given String, replace it’s words from lookup dictionary.

Input : test_str = ‘geekforgeeks best for geeks’, repl_dict = {“geeks” : “all CS aspirants”} 
Output : geekforgeeks best for all CS aspirants 
Explanation : “geeks” word is replaced by lookup value. 

Input : test_str = ‘geekforgeeks best for geeks’, repl_dict = {“good” : “all CS aspirants”} 
Output : geekforgeeks best for geeks 
Explanation : No lookup value, unchanged result.

Method #1 : Using split() + get() + join()

In this, we initially split the list using split(), then look for lookups using get(), and if found, replaced and joined back to string using join().

Python3




# Python3 code to demonstrate working of
# Replace words from Dictionary
# Using split() + join() + get()
 
# initializing string
test_str = 'geekforgeeks best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# lookup Dictionary
lookp_dict = {"best" : "good and better", "geeks" : "all CS aspirants"}
 
# performing split()
temp = test_str.split()
res = []
for wrd in temp:
     
    # searching from lookp_dict
    res.append(lookp_dict.get(wrd, wrd))
     
res = ' '.join(res)
 
# printing result
print("Replaced Strings : " + str(res))


Output

The original string is : geekforgeeks best for geeks
Replaced Strings : geekforgeeks good and better for all CS aspirants

Time Complexity: O(N), where N is the length of the input string.

Auxiliary Space: O(N)

Method #2 : Using list comprehension + join()

Similar to above method, difference just being 1 liner rather than 3-4 steps in separate lines.

Python3




# Python3 code to demonstrate working of
# Replace words from Dictionary
# Using list comprehension + join()
 
# initializing string
test_str = 'geekforgeeks best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# lookup Dictionary
lookp_dict = {"best" : "good and better", "geeks" : "all CS aspirants"}
 
# one-liner to solve problem
res = " ".join(lookp_dict.get(ele, ele) for ele in test_str.split())
 
# printing result
print("Replaced Strings : " + str(res))


Output

The original string is : geekforgeeks best for geeks
Replaced Strings : geekforgeeks good and better for all CS aspirants

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

Method #3: Using a for loop and a temporary list

  • Initialize the lookup dictionary variable lookp_dict with the key-value pairs “best” : “good and better” and “geeks” : “all CS aspirants”.
  • Create an empty list called temp that will hold the replaced strings.
  • Split the input string into a list of words using the split() method and iterate over each word using a for loop.
  • For each word, check if it exists in the lookup dictionary using the get() method. If it does, append the corresponding value to the temp list, otherwise append the original word.
  • Join the temp list using the join() method and separate the words with a space character. Assign this string to the variable res.
  • Print the final result using print(“Replaced Strings : ” + str(res)).

Python3




# initializing string
test_str = 'geekforgeeks best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# lookup Dictionary
lookp_dict = {"best" : "good and better", "geeks" : "all CS aspirants"}
 
# create a temporary list to hold the replaced strings
temp = []
for word in test_str.split():
    temp.append(lookp_dict.get(word, word))
 
# join the temporary list to create the final output string
res = " ".join(temp)
 
# printing result
print("Replaced Strings : " + str(res))


Output

The original string is : geekforgeeks best for geeks
Replaced Strings : geekforgeeks good and better for all CS aspirants

Time complexity: O(n), where n is the number of words in the input string.
Auxiliary space: O(n), where n is the number of words in the input string (due to the creation of the temporary list).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads