three argument filtering
Lets assume I have defined function:
def threeArgs(x,y,z): return ... # Returns True or False
I also have a list of elements:
a = [elem1, elem2, ..., elemN]
I would like to construct another list b containing first and last element form list a and those elements Xn in between for which threeArgs(Xn-1,Xn, Xn+1) returns true.
How to do it in elegant pythonic way?
Use slicing to create three shifted lists, then zip them together:
[a] + [y for x, y, z in zip(a, a[1:], a[2:]) if threeArgs(x, y, z)] + [a[-1]]