replacing loops with list comprehensions