Given list of Strings, replace the value mapped by 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 one-liner in list comprehension.
Python3
# Python3 code to demonstrate working of # Replace String by Kth Dictionary value # Using list comprehension # initializing list test_list = ["Gfg", "is", "Best"] # printing original list print("The original list : " + str(test_list)) # initializing subs. Dictionary subs_dict = { "Gfg" : [5, 6, 7], "is" : [7, 4, 2], } # initializing K K = 2 # using list comprehension to solve # problem using one liner res = [ele if ele not in subs_dict else subs_dict[ele][K] for ele in test_list] # printing result print("The list after substitution : " + str(res)) |
The original list : ['Gfg', 'is', 'Best'] The list after substitution : [7, 2, 'Best']
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
# Python3 code to demonstrate working of # Replace String by Kth Dictionary value # Using get() + list comprehension # initializing list test_list = ["Gfg", "is", "Best"] # printing original list print("The original list : " + str(test_list)) # initializing subs. Dictionary subs_dict = { "Gfg" : [5, 6, 7], "is" : [7, 4, 2], } # initializing K K = 2 # using list comprehension to solve problem using one liner # get() to perform presence checks and assign default value res = [subs_dict.get(ele, ele) for ele in test_list] res = [ele[K] if isinstance(ele, list) else ele for ele in res] # printing result print("The list after substitution : " + str(res)) |
The original list : ['Gfg', 'is', 'Best'] The list after substitution : [7, 2, 'Best']
Recommended Posts:
- Python - Replace dictionary value from other dictionary
- Python - Test Kth index in Dictionary value list
- Python - How to Sort a Dictionary by Kth Index Value
- Python - Extract Kth index elements from Dictionary Value list
- Python - Kth word replace in String
- Python | Pretty Print a dictionary with dictionary value
- Python - Convert Dictionary Value list to Dictionary List
- replace() in Python to replace a substring
- Python | Pandas Series.str.replace() to replace text in a series
- Python | Convert string dictionary to dictionary
- Python - Extracting Kth Key in Dictionary
- Python - Replace words from Dictionary
- Python - Extract ith Key's Value of K's Maximum value dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

