Optimizing by using grep instead of python filter