I have two lists:
a = ['first', '___' , 'third', '___' ]
b = ['second', 'fourth']
I want to unpack b
into a
to replace the special character ___
while keeping the current order.
The result should be
res = ['first', 'second' , 'third', 'fourth']
What is the most efficient way to do that?
CodePudding user response:
One approach is to use next
:
a = ['first', '___', 'third', '___']
b = ['second', 'fourth']
iter_b = iter(b)
res = [next(iter_b) if ei == '___' else ei for ei in a]
print(res)
Output
['first', 'second', 'third', 'fourth']