I made a list of indices of an array by
indexlist = [(x, y) for x, y in np.ndindex(a.shape)]
a is a 4x7 array. I was trying to figure out a way to get a list of every index in a particular column, except for one particular index in that column (In this example, index of interest is (1,2). I used this:
for i in indexlist:
if i[1] != pivot[1]:
indexlist.remove(i)
print(i)
(pivot = (1,2)). I expected this to give me a list with only (0,2)(1,2)(2,2)(3,2), but I get a much larger list. When I run it again, it removes a few more values. It gets to what I was expecting, but only after I run it 3 times. Why?
for i in indexlist:
if i[1] != pivot[1]:
indexlist.remove(i)
print(i)
(0, 0)
(0, 3)
(0, 5)
(1, 0)
(1, 3)
(1, 5)
(2, 0)
(2, 3)
(2, 5)
(3, 0)
(3, 3)
(3, 5)
for i in indexlist:
if i[1] != pivot[1]:
indexlist.remove(i)
print(i)
(0, 1)
(0, 4)
(1, 1)
(1, 4)
(2, 1)
(2, 4)
(3, 1)
(3, 4)
for i in indexlist:
if i[1] != pivot[1]:
indexlist.remove(i)
print(i)
(0, 6)
(1, 6)
(2, 6)
(3, 6)
for i in indexlist:
if i[1] != pivot[1]:
indexlist.remove(i)
print(i)
print(indexlist)
[(0, 2), (1, 2), (2, 2), (3, 2)]
Clarification, I want to remove items from the list, but I do not understanmd why it doesn't remove the items (x,i) where i != 2 all at once.