Refined the code. Added for loops for pythonic way