Hi I am pretty new at coding and I was looking for some tips to make my code look more clean (improve my style)
Is there a better way to loop twice rather than using 2 for loops?
The code is supposed to do this.
def solution(matrix):
new_matrix=[]
for i in range(len(matrix)):
new_matrix.append([])
for j in range(len(matrix[0])):
new_matrix[i].append(0)
for i in range(len(matrix)):
for j in range(len(matrix[0])):
for k in [-1,0,1]:
for l in [-1,0,1]:
if 0<=i+k<=len(matrix)-1 and 0<=j+l<=len(matrix[0])-1:
if (matrix[i+k][j+l]==True) and ((i,j)!=(i+k,j+l)):
new_matrix[i][j]+=1
return new_matrix