Here is a peak detection routine which works as I want it. However, I want to make it more flexible.

```
def peak2(x,y,dp,dv):

# Define two arrays: one for the peaks and one
# for the valleys

  peaks=[]
  valleys=[]

# Create two arrays, one for x, and one for y, where each
# element of the new array # consists of three adjacent
# elements of the old array.

  xt=zip(x,x[1:],x[2:])
  yt=zip(y,y[1:],y[2:])

# Walk through these arrays, checking to see if the middle
# value of the three old elements exceeds its neighbors by
# d or more.

  idx=1
  for i,j in zip(xt,yt):
    if(j[1]-j[0]>dp and j[1]-j[2]>dp):
      peaks.append((x[idx],y[idx]))
    elif (j[0]-j[1]>dv and j[2]-j[1]>dv):
      valleys.append((x[idx],y[idx]))
    idx+=1

  return array(peaks),array(valleys)

```

As you can see, it detects a peak by comparing a value with its right and left neighbor. And if the center value is greater than both its immediate neighbors by a certain threshold, then it is considered a peak. Similar logic for finding a valley.

I want to expand it so that it compares the center value with n neighbors on each side. I will pass a parameter to the function (call it `w`), and if `w=3`, then I do something like this:

```
xt=zip(x,x[1:],x[2:])
yt=zip(y,y[1:],y[2:])

```

which is what is currently in the routine. But if `w=5`, then I want this:

```
xt=zip(x,x[1:],x[2:],x[3:],x[4:])
yt=zip(y,y[1:],y[2:],y[3:],y[4:])

```

And if `w=n`, where `n` is odd, then I want this:

```
xt=zip(x,x[1:],x[2:],...,x[n:])
yt=zip(y,y[1:],y[2:],...,y[n:])

```

So how can I build these arrays where each element contains `n` elements of other arrays?