Shortened code by incorporating list and dict comprehension