You can't remove an item from a list while iterating the list. When the item is removed, the remaining items in the list move one position toward the head of the list. The next item in the list is now at the same position in the list where the deleted item was. The next iteration of the for loop moves to the next
position in the list which will contain the item 2 places after the deleted item
a = ['1','2','3','4','5']
for i in a:
print(i, end=' ')
if i == '2':
a.remove(i)
prints 1 2 4 5. Here's why:
a: 1,2,3,4,5
^ (iterator pointer)
prints 1
next iteration
a: 1,2,3,4,5
^
prints 2
removes 2
a: 1,3,4,5
^
next iteration
a: 1,3,4,5
^
prints 4
next iteration
a: 1,3,4,5
^
prints 5
For the assembler, you want to do identical parsing on identical input data for both passes. Your
line variable would be better named something like
addr since it represents the instruction address associated with the line being parsed. It should not be incremented for labels since labels do not result in any instructions.
--Mark