In python, what’s a clean way to insert an element between any two elements that meet a condition?
A call like:
insert_between([1,2,3,4,7,8,9,15,16], 0, lambda x,y: x + 1 != y)
should produce:
[1,2,3,4,0,7,8,9,0,15,16]
Is there a better way than to iterate and append to second list?
This is pretty much as efficient as you’ll get, because you’re going to have to make one pass through the list anyway and this makes only one pass. Notice that it is a generator, so you need to cast it to a list if you need all the values at once.