loop optimization; run time improved from ~300s to 0.1sec
rewrite to use list comprehensions instead of loops