Jimit Raithatha Jimit Raithatha - 6 months ago 11
Python Question

Python: How to replace substring occurrences with next values from list?

I have the following string and list:

myString = "a:::b:::c:::d ..... " where ':::' is sort of delimiter
myList = [1,2,3,4.......]


I know how to cycle through list values, but how do i replace each occurrence of ':::' with next value in list, such that my final output is as follows:

myString = "a1b2c3d ...."

Answer

Quick & Dirty:

myString = "a:::b:::c:::d"
myList = [1,2,3,4]

s = iter(myString.split(":::"))

print(next(s) + "".join(str(y)+x for x,y in zip(s,myList)))

prints a1b2c3d