Simplify python code