Sympy has built-in functions for Jacobian (gradient) and Hessian
Given your function lamb and variable-matrix x:
lamb
x
from sympy import Matrix Matrix([lamb]).jacobian(x)
from sympy import hessian hessian(lamb, x)