A pythonic way of solving the problem