'replacement of special cases from the list [duplicate]
I want to map only specific strings from the list, for example: "TV" -> "television", "PC" -> "computer".
initial list:
["TV", "PC", "keyboard", "screen"]
expected list:
["television", "computer", "keyboard", "screen"]
So I came up with this idea using comprehension list:
dct = {
"TV": "television",
"PC": "computer"
}
lst = ["TV", "PC", "keyboard", "screen"]
mapped_lst = [i if i not in dct else dct[i] for i in lst]
But I'm still wondering if there is any better way to do it? In my real life problem I expect large list with several dozen values to map.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
