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.
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.
OutputThe original list : ['Gfg', 'is', 'Best']
The list after substitution : [7, 2, 'Best']
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().
OutputThe original list : ['Gfg', 'is', 'Best']
The list after substitution : [7, 2, 'Best']
OutputThe original list : ['Gfg', 'is', 'Best']
The list after substitution : [7, 2, 'Best']
OutputThe original list : ['Gfg', 'is', 'Best']
The list after substitution : [7, 2, 'Best']
OutputThe original list : ['Gfg', 'is', 'Best']
The list after substitution : [7, 2, 'Best']