What is the best way to fix the error given in the run? I also somewhat understand that a list cannot change while being iterated over, but it still seems a little abstract.
This is what list.pop is for!
myList = ["A", "B", "C", "D", "E", "F", "G"]
and remove the second element with:
myList.pop(2)
which will modify the list
to:
['A', 'B', 'D', 'E', 'F', 'G']
As pointed out in the comments, modifying a list
that you are iterating
over is never a good idea. If something more complicated but similar to this had to be done, you would make a copy of the list
first with myList[:]
and then iterate through changing the copy
. But for this scenario, list.pop
is the definitely the right option.