0

I tried this:

mylist = range(20)

for item in mylist:
    mylist.remove(item)

len(mylist) == 20

Why the result is not [] ?

longnight
  • 162
  • 2
  • 11
  • .remove returns a copy of the list. Use mylist = mylist.remove(item) – Stiffo Jul 29 '15 at 08:54
  • 2
    Repeat after me. Never, ever modify the list that you are iterating over, it causes all kinds of problems. Rather, iterate over the original list and modify a copy (using the index). – Bas Jansen Jul 29 '15 at 08:55
  • No , .remove() returns a None, and modified the list. @Stiffo – longnight Jul 29 '15 at 09:32

0 Answers0