for loop - Python: Iterating over sub-lists -
i have project trying edit portions of nested lists. started list:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
i want iterate on portion of list output square of ones in center so.
[[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]]
i tried using for-loop iterate through list , nested loop iterate through sub-lists. however, did not work. got instead list:
[[0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0]]
here code:
list = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] in range(1,4): j in range(1,4): list[i][j] = 1
why won't code work? have searched day or 2 , have not found answer. thank in advance whoever takes time answer or comment.
the code posted working fine:
>>> list = [[0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0]] >>> >>> in range(1,4): ... j in range(1,4): ... list[i][j] = 1 ... >>> pprint(list) [[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]]
check code looks posted here.
Comments
Post a Comment