Python – Replace String by Kth Dictionary value
Last Updated :
05 Apr, 2023
Given a list of Strings, replace the value mapped with the Kth value of mapped list.
Input : test_list = [“Gfg”, “is”, “Best”], subs_dict = {“Gfg” : [5, 6, 7], “is” : [7, 4, 2]}, K = 0
Output : [5, 7, “Best”]
Explanation : “Gfg” and “is” is replaced by 5, 7 as 0th index in dictionary value list.
Input : test_list = [“Gfg”, “is”, “Best”], subs_dict = {“Gfg” : [5, 6, 7], “Best” : [7, 4, 2]}, K = 0
Output : [5, “is”, 7]
Explanation : “Gfg” and “Best” is replaced by 5, 7 as 0th index in dictionary value list.
Method #1: Using list comprehension
This is one of the ways in which this task can be performed. In this, we perform the task iteration and conditional replacement inside a one-liner in list comprehension.
Python3
test_list = [ "Gfg" , "is" , "Best" ]
print ( "The original list : " + str (test_list))
subs_dict = {
"Gfg" : [ 5 , 6 , 7 ],
"is" : [ 7 , 4 , 2 ],
}
K = 2
res = [ele if ele not in subs_dict else subs_dict[ele][K]
for ele in test_list]
print ( "The list after substitution : " + str (res))
|
Output
The original list : ['Gfg', 'is', 'Best']
The list after substitution : [7, 2, 'Best']
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using get() + list comprehension
The combination of above functions can be used to solve this problem. In this, we iterate using list comprehension and check for key existence and substitution using get().
Python3
test_list = [ "Gfg" , "is" , "Best" ]
print ( "The original list : " + str (test_list))
subs_dict = {
"Gfg" : [ 5 , 6 , 7 ],
"is" : [ 7 , 4 , 2 ],
}
K = 2
res = [subs_dict.get(ele, ele) for ele in test_list]
res = [ele[K] if isinstance (ele, list ) else ele for ele in res]
print ( "The list after substitution : " + str (res))
|
Output
The original list : ['Gfg', 'is', 'Best']
The list after substitution : [7, 2, 'Best']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using a loop
Use a for loop to iterate through the list and check if the current element is present in the subs_dict. If it is, then replace the current element with the Kth value from the dictionary.
Python3
test_list = [ "Gfg" , "is" , "Best" ]
print ( "The original list : " + str (test_list))
subs_dict = {
"Gfg" : [ 5 , 6 , 7 ],
"is" : [ 7 , 4 , 2 ],
}
K = 2
for i in range ( len (test_list)):
if test_list[i] in subs_dict:
test_list[i] = subs_dict[test_list[i]][K]
print ( "The list after substitution : " + str (test_list))
|
Output
The original list : ['Gfg', 'is', 'Best']
The list after substitution : [7, 2, 'Best']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using map() function
Another way to replace the strings in a list with the k-th value from the dictionary is to use the map() function, which takes a function and an iterable as arguments and applies the function to each element of the iterable.
Here’s how you can use map() to solve this problem:
- Define a function replace_string that takes a string s as input and returns either the value at the k-th index of the dictionary if the string is a key in the dictionary, or the original string if it is not.
- Use the map() function to apply replace_string to each element of the list test_list.
- Convert the result of the map() function back to a list using the list() function.
- Print the resulting list.
Python3
test_list = [ "Gfg" , "is" , "Best" ]
print ( "The original list : " + str (test_list))
subs_dict = {
"Gfg" : [ 5 , 6 , 7 ],
"is" : [ 7 , 4 , 2 ],
}
K = 2
def replace_string(s):
return subs_dict[s][K] if s in subs_dict else s
res = list ( map (replace_string, test_list))
print ( "The list after substitution : " + str (res))
|
Output
The original list : ['Gfg', 'is', 'Best']
The list after substitution : [7, 2, 'Best']
Time complexity: O(n), where n is the length of the input list test_list since we need to apply the replace_string function to each element of the list.
Auxiliary space: O(n), since we create a new list res with the same length as test_list to store the result.
Method #7: Using a dictionary comprehension
Step-by-step approach:
- Use a dictionary comprehension to replace each string in test_list with its Kth element in subs_dict, if the string is present as a key in the dictionary, otherwise, leave the string unchanged.
- Store the resulting list in the variable res.
- Print the updated list res.
Below is the implementation of the above approach:
Python3
test_list = [ "Gfg" , "is" , "Best" ]
print ( "The original list : " + str (test_list))
subs_dict = {
"Gfg" : [ 5 , 6 , 7 ],
"is" : [ 7 , 4 , 2 ],
}
K = 2
res = [subs_dict[x][K] if x in subs_dict else x for x in test_list]
print ( "The list after substitution : " + str (res))
|
Output
The original list : ['Gfg', 'is', 'Best']
The list after substitution : [7, 2, 'Best']
Time complexity: O(n), where n is the length of the input list
Auxiliary space: O(n), since we are creating a new list of the same length as the input list.
Please Login to comment...