sheth7
sheth7

Reputation: 349

match and replace string items in list with string items from another list

I have three lists - base, match, and replac ; match and replac are same length

base = ['abc', 'def', 'hjk']

match = ['abc', 'hjk']

replac = ['abcde', 'hjklm']

I would like to modify the base list by matching string items in match and replace these with the same index item from replac.

Expected output: base = ['abcde', 'def', 'hjklm']

Upvotes: 0

Views: 37

Answers (1)

yukashima huksay
yukashima huksay

Reputation: 6238

Here is how I'd do it:

mapp = dict(zip(match,replac))
res = [mapp[e] if e in mapp else e for e in base]

Upvotes: 2

Related Questions