I have a 2 dimensional list:
[[5, 80, 2, 57, 5, 97], [2, 78, 2, 56, 6, 62], [5, 34, 3, 54, 6, 5, 2, 58, 5, 61, 5, 16]]
In which I need to change every second element to 0, starting from the first. Therefore, it should look like this:
[[0, 80, 0, 57, 0, 97], [0, 78, 0, 56, 0, 62], [0, 34, 0, 54, 0, 5, 0, 58, 0, 61, 0, 16]]
The algorithm I use is:
for i in tempL: for j, item in enumerate(i): if i.index(item) % 2 == 0: print('change, index:'), print(i.index(item)) i[j] = 0 else: print('not change, index:'), print(i.index(item))
But I get the following:
change, index: 0 not change, index: 1 change, index: 2 not change, index: 3 change, index: 4 not change, index: 5 change, index: 0 not change, index: 1 change, index: 2 not change, index: 3 change, index: 4 not change, index: 5 change, index: 0 not change, index: 1 change, index: 2 not change, index: 3 change, index: 4 not change, index: 5 change, index: 6 not change, index: 7 not change, index: 5 not change, index: 9 not change, index: 5 not change, index: 11 [[0, 80, 0, 57, 0, 97], [0, 78, 0, 56, 0, 62], [0, 34, 0, 54, 0, 5, 0, 58, 5, 61, 5, 16]]
Some elements do not change, and because (I added an index print to see this), he believes that the index of these elements for some reason is 7 and 9. What could be because I searched for the error for so long, until I can to find.
I double-checked that there were no extra spaces or anything in the list.