Q:

How to elegantly replace certain characters in one string with characters in another at the same index?

I want to replace each instance of a character in one string with a character at the same index in another string. If no character exists at that index, leave it as it is.
Here's my solution with a list comprehension (Python 3):
string1 = "food is delicious"
string2 = "orange is not delicious"
string3 = "".join([string2[i] if i<len(string2) and c=="o" else c for i, c in enumerate(string1)])
print(string3)

Result
frad is delicidus

It feels like there should be a better way though, for example using str.replace somehow. Any ideas?

A:

You can itertools.zip_longest to iterate both the strings, till the longest of the them is exhausted. The smaller string will be filled with fillvalue
>>> s1 = "food is delicious"
>>> s2 = "orange is not delicious"

>>> from itertools import zip_longest
>>> "".join([c2 if (c1 == 'o' and c2) else c1 for c1, c2 in zip_longest(s1, s2, fillvalue='')])
'frad is delicidus'

