I have many (> 100,000) string lines in a list where a subset might look like this:
str_list = ["hello i am from denmark", "that was in the united states", "nothing here"]
I also have such a dict (in reality it will have a length of about ~ 1000):
dict_x = {"denmark" : "dk", "germany" : "ger", "norway" : "no", "united states" : "us"}
For all the lines in the list that contain any of the dict keys, I want to replace the whole line with the corresponding dict value. The expected result should be like this:
str_list = ["dk", "us", "nothing here"]
What is the most efficient way to do this, given the number of lines I have and the length of the dict?
Additional information: in a line no more than one key of a key.
source
share