Implementation of grep in python