Probably the main reason the tutorial tells you to make a shallow copy is because of the sometimes unexpected behavior of deleting an object in a for loop (for example you could get an out of bounds error depending on the exact implementation).
You should probably stick to the original version or look up other ways to remove elements from an array in a loop.
Example #1 - works
For example, this example removes all even numbers from a list:
arr = [i for i in range(10)]
print(arr) #prints: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for x in arr:
if x%2==0:
arr.remove(x)
print(arr)#prints: [1, 3, 5, 7, 9]
Example #2 - IndexError
This attempts to do the same thing but throws an index error:
arr = [i for i in range(10)]
for x in range(len(arr)):
if arr[x]%2==0:
arr.remove(arr[x])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-3-2b0b17a4b250> in <module>
10 arr = [i for i in range(10)]
11 for x in range(len(arr)):
---> 12 if arr[x]%2==0:
13 arr.remove(arr[x])
IndexError: list index out of range
But you can get it to work by operating on a shallow copy:
import copy
arr = [i for i in range(10)]
c = copy.copy(arr) #create a shallow coppy
for x in range(len(arr)):
if c[x]%2==0:# make accesses only to the copy
arr.remove(c[x])#delete only by value (and not by index)
print(arr)#prints: [1, 3, 5, 7, 9]