Iterating a list to delete values less than target
I am trying to iterate numList and delete all values less than 8 (target). Both 2 & 5 are removed correctly but 3 & 7 are not.
- It is definitely the remove method. If numList.remove(n) commented out, the program print statements run correctly.
numList = [2, 3, 5, 7, 11, 13, 17, 19]
for n in numList:
print('Testing: {}'.format(n))
if n < 8:
print('-----REMOVING: {}'.format(n))
numList.remove(n)
EXPECTED RESULTS:
Testing: 2
-----REMOVING: 2
Testing: 3
-----REMOVING: 3
Testing: 5
-----REMOVING: 5
Testing: 7
-----REMOVING: 7
Testing: 11
Testing: 13
Testing: 17
Testing: 19
Expecting: [11, 13, 17, 19]
ACTUAL RESULTS
Testing: 2
-----REMOVING: 2
Testing: 5
-----REMOVING: 5
Testing: 11
Testing: 13
Testing: 17
Testing: 19
Actual: [3, 7, 11, 13, 17, 19]