2

While initially creating a kind of queue for something else I encountered a weird "problem": If I iterate over the queue and would remove every element via the list.remove(x) command I expect the empty list to be the result. But it doesn't, instead in returns basically a sublist as far as I can see.

test = ['1', '4', '3', '5']
for entry in test:
    test.remove(entry)
print(test) # Prints ['4', '5']

The official documentation for list.remove(x) states:

Remove the first item from the list whose value is x. It is an error if there is no such item.

I tried it with other examples (i.e. '1', '4', '3' prints '4' or '1', '2', '3' prints '2') but the "error" occurs every time. Am I missing something obvious? Or is the command supposed to be used in another way?

1
  • @DanielPryden You are right. Voting to close now. Should've done better research... Commented Mar 9, 2019 at 2:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.