0
number3=[30, 32, 34, 36, 50, 52, 54, 90, 300, 302, 303, 304, 305, 306, 320, 322, 323, 324, 325, 340, 342, 343, 360, 500, 502, 503, 504, 505, 520, 522, 523, 540, 900]
for i in (number3):
    m=str(i)
    n=len(m)
    if n == 2:
        number3.remove(i)
    else:
        pass

I don't know why, but the interpreter only processes the first number. I want it go through the whole list and remove the number which have 2 digits

  • the proper syntax of the for loop would be: `for i in number3:` – zlr Apr 19 '14 at 21:46
  • possible duplicate of [Loop "Forgets" to Remove Some Items](http://stackoverflow.com/questions/17299581/loop-forgets-to-remove-some-items) – Blckknght Apr 19 '14 at 22:06

1 Answers1

3

You shouldn't try to modify a list while iterating over it. It's much easier to use a list comprehension to do this:

number3 = [n for n in number3 if len(str(n)) != 2]
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437