T
T
Tayrus02020-05-16 10:48:38
Python
Tayrus0, 2020-05-16 10:48:38

Why are my items not being removed from the list?

There is this code:

text_to_transform = ['(Это) ', '(те)@(стовый) ', '(тек)@(ст)']
for x in text_to_transform:
    if '@' in x:
        text_to_transform.remove(x)
print(text_to_transform)


I need to remove all elements from the list that contain '@' in themselves, but at the output I get why this element was not deleted? It after all contains '@' in itself. Only the element should remain in the list['(Это) ', '(тек)@(ст)']( (тек)@(ст) )'(Это)'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2020-05-16
@Tayrus0

You are iterating the list and editing it at the same time. Do not do it this way. At least here:
for x in text_to_transform.copy():

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question