optimize code by using faster python function