Removing from a list while iterating over it

后端 未结 4 1648
走了就别回头了
走了就别回头了 2020-11-22 03:05

The following code:

a = list(range(10))
remove = False
for b in a:
    if remove:
        a.remove(b)
    remove = not remove
print(a)

Outp

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 04:01

    I debated answering this for a while, because similar questions have been asked many times here. But it's just unique enough to be given the benefit of the doubt. (Still, I won't object if others vote to close.) Here's a visual explanation of what is happening.

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]       <-  b = 0; remove? no
     ^
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]       <-  b = 1; remove? yes
        ^
    [0, 2, 3, 4, 5, 6, 7, 8, 9]          <-  b = 3; remove? no
           ^
    [0, 2, 3, 4, 5, 6, 7, 8, 9]          <-  b = 4; remove? yes
              ^
    [0, 2, 3, 5, 6, 7, 8, 9]             <-  b = 6; remove? no
                 ^
    [0, 2, 3, 5, 6, 7, 8, 9]             <-  b = 7; remove? yes
                    ^
    [0, 2, 3, 5, 6, 8, 9]                <-  b = 9; remove? no
                       ^
    

    Since no one else has, I'll attempt to answer your other questions:

    Why is no error given to indicate that underlying iterator is being modified?

    To throw an error without prohibiting many perfectly valid loop constructions, Python would have to know a lot about what's going on, and it would probably have to get that information at runtime. All that information would take time to process. It would make Python a lot slower, in just the place where speed really counts -- a loop.

    Have the mechanics changed from earlier versions of Python with respect to this behaviour?

    In short, no. Or at least I highly doubt it, and certainly it has behaved this way since I learned Python (2.4). Frankly I would expect any straightforward implementation of a mutable sequence to behave in just this way. Anyone who knows better, please correct me. (Actually, a quick doc lookup confirms that the text that Mikola cited has been in the tutorial since version 1.4!)

提交回复
热议问题