Use numpy.diff
If dx is constant
from numpy import diff
dx = 0.1
y = [1, 2, 3, 4, 4, 5, 6]
dy = diff(y)/dx
print dy
array([ 10., 10., 10., 0., 10., 10.])
dx is not constant (your example)
from numpy import diff
x = [.1, .2, .5, .6, .7, .8, .9]
y = [1, 2, 3, 4, 4, 5, 6]
dydx = diff(y)/diff(x)
print dydx
[10., 3.33333, 10. , 0. , 10. , 10.]
Note that this approximated “derivative” has size n-1 where n is your array/list size.
Don’t know what you are trying to achieve but here are some ideas:
- If you are trying to make numerical differentiation maybe finite differences formulation might help you better.
- The solution above is like a first-order accuracy approximation for the forward schema of finite differences with a non-uniform grid/array.