implementing transform function in Python