mardi 4 décembre 2018

Remove Command After Conditional Logic Does Not Behave as Expected Python

list1 = ['test@test.com', 'number@test.com', 'letters@test.com', 'fu@bar.com']
for address in list1:
    if 'TEST.COM' in address.upper():
        print('Positive: ' + address)

Given the above code, you receive the following output

Positive: test@test.com
Positive: number@test.com
Positive: letters@test.com

However, if I change the conditional logic to remove the addresses that adhere to the conditions, I receive a list that never removes all the addresses I expect.

for address in list1:
    if 'TEST.COM' in address.upper():
        list1.remove(address)

Yields

list1 = ['number@test.com', 'fu@bar.com']

Why is number@test.com still an item in the list when the code should have removed it? Am I missing something? I've researched this, but am having trouble finding why this could be happening.

Aucun commentaire:

Enregistrer un commentaire