replaced the for loops with 1 loop using itertools