Making the code more pythonic