I have some Vector2/Array code that has me stumped...

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By NapalmNate

I have this “simple” bit of code and I’m not sure why it isn’t working…

for i in Build_Location_Array:
	if i.x == Build_Location_Start.x or i.y == Build_Location_Start.y:
		print("x" + str(i.x) + " LocationStartX/" + str(Build_Location_Start.x) + " : y" + str(i.y) + " LocationStartY/" + str(Build_Location_Start.y))
		Build_Location_Array.erase(i);
		Token_Array.erase(i);
	print(Build_Location_Array)

It’s just supposed to remove the first row of the x and y axis on a Vector2(4,4). But, for some reason that I haven’t figured out, it only removes five of the seven items that it’s supposed to remove in the array.

The debug:

x23 LocationStartX/23 : y1 LocationStartY/1
x23 LocationStartX/23 : y3 LocationStartY/1
x24 LocationStartX/23 : y1 LocationStartY/1
x25 LocationStartX/23 : y1 LocationStartY/1
x26 LocationStartX/23 : y1 LocationStartY/1
[(23, 2), (23, 4), (24, 2), (24, 3), (24, 4), (25, 2), (25, 3), (25, 4), (26, 2), (26, 3), (26, 4)]

if you notice in the array printout: (23,2) and (23,4) are still there although they should have been removed. (At least I think they should have.)

Does anyone see it? Cuz, I don’t see it!

Discovery:

After a bit more testing, I decided to just (copy/paste) run the same bit of code two more times. All variables stayed the same during the three runs of the above code.(except for "Build_Location_Array)
These were the results…(Still confused.):

x23 LocationStartX/23 : y1 LocationStartY/1
x23 LocationStartX/23 : y3 LocationStartY/1
x24 LocationStartX/23 : y1 LocationStartY/1
x25 LocationStartX/23 : y1 LocationStartY/1
x26 LocationStartX/23 : y1 LocationStartY/1
[(23, 2), (23, 4), (24, 2), (24, 3), (24, 4), (25, 2), (25, 3), (25, 4), (26, 2), (26, 3), (26, 4)]
x23 LocationStartX/23 : y2 LocationStartY/1
[(23, 4), (24, 2), (24, 3), (24, 4), (25, 2), (25, 3), (25, 4), (26, 2), (26, 3), (26, 4)]
x23 LocationStartX/23 : y4 LocationStartY/1
[(24, 2), (24, 3), (24, 4), (25, 2), (25, 3), (25, 4), (26, 2), (26, 3), (26, 4)]

:bust_in_silhouette: Reply From: jgodfrey

Your problem is likely related to the fact that you’re removing elements from an array that your actively iterating over. When an element is removed, everything in the structure shifts and likely confuses the iterator.

To correct that, you can do one of…

  1. Iterate over the array in reverse order by index. That way, when
    you remove an element, it won’t impact the remaining elements you’re
    still processing…
  2. Process the array in 2 steps.
    a. Iterate over the array and simply record the elements you want to remove
    b. Remove the elements directly, without any iteration…

jgodfrey swoops in to save the day!

That was 100% the problem. I didn’t realize that elements within a loop could be shifted around. Seems completely reasonable now, and after implementing solution (2), things are working as intended.

Thanks a bunch. I probably would have been stuck for at least a week over something so simple.

NapalmNate | 2020-10-23 18:31