RE: modifying mutable list elements in a for loop

Discussion in 'Python' started by Michael Chermside, May 26, 2004.

  1. Peter Ballard writes:
    > The python tutorial tells me "It is not safe to modify the sequence
    > being iterated over in the loop".

    [... tried modifying the list elements and it worked ...]
    > because how else can one perform an operation on a list of objects?
    > But that phrase "It is not safe to modify the sequence being iterated
    > over in the loop" in the tutorial has me slightly worried.


    Actually, you're far more worried than necessary. The Python tutorial
    is written for beginners, and the stricture to avoid modifying a
    sequence being iterated over is good advice for beginners, rather
    than an actual rule of the Python language.

    First of all, your analysis is correct: it is always OK to modify the
    items in a list during a loop. You are even correct that what the
    list "really" contains (at the C level) is just pointers to the items
    in the list.

    But the real truth goes even further. There are some data structures
    in some languages which you *MUST NOT* modify while traversing them
    because for some reason (probably having to do with threading) it
    would result in undefined behavior. But that is NOT a problem with
    Python. In Python it's perfectly OK to modify a list (or dict, or
    whatever) while traversing it, it's just not a good idea for
    beginners (or experienced programmers who want to produce
    understandable code) because the behavior may not be what you expect.

    >>> alist = range(10)
    >>> for x in alist:

    ... alist.remove(x)
    ...
    >>> alist

    [1, 3, 5, 7, 9]

    This behavior is perfectly well defined. (Think about it. Now think
    about it again. Got it?) But you have to admit that it's confusing
    to beginners, so the Tutorial strongly recomends against it.

    -- Michael Chermside
     
    Michael Chermside, May 26, 2004
    #1
    1. Advertising

  2. Michael Chermside

    Roy Smith Guest

    Michael Chermside <> wrote:
    > >>> alist = range(10)
    > >>> for x in alist:

    > ... alist.remove(x)
    > ...
    > >>> alist

    > [1, 3, 5, 7, 9]
    >
    > This behavior is perfectly well defined. (Think about it. Now think
    > about it again. Got it?) But you have to admit that it's confusing
    > to beginners, so the Tutorial strongly recomends against it.


    It's confusing to non-beginners too. I've been using Python for 7 years
    now. While I don't spend a lot of time delving into the esoterica of
    the language, I think I've got a pretty good handle on how it works.

    I think I understand what's going on in the loop, but there's a
    difference between being able to understand why it's doing something vs.
    being able to look at a piece of code and predict what it's going to do.
    I don't think I would have been able to do that with this code.

    So, my recommendation is to stay away from tricky things like this. The
    same thing could have been done with:

    >>> temp = range (10)
    >>> alist = temp [1::2]
    >>> alist

    [1, 3, 5, 7, 9]

    or for that matter:

    >>> range (1, 10, 2)

    [1, 3, 5, 7, 9]

    but I suspect the point of the example was not how to create such a list
    from scratch, but how to get every other element of an existing list.

    Never try to be fancy. Someday, somebody's going to have to read your
    code and they'll curse you for it. That somebody might even be you.
     
    Roy Smith, May 26, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Ballard
    Replies:
    6
    Views:
    399
    Peter Hansen
    May 27, 2004
  2. Chris Lasher

    Priority Queue with Mutable Elements

    Chris Lasher, Jun 15, 2007, in forum: Python
    Replies:
    3
    Views:
    547
    Scott David Daniels
    Jun 16, 2007
  3. Replies:
    6
    Views:
    460
    Andrey Tarasevich
    Feb 22, 2008
  4. Isaac Won
    Replies:
    9
    Views:
    391
    Ulrich Eckhardt
    Mar 4, 2013
  5. Matt
    Replies:
    67
    Views:
    430
    glen herrmannsfeldt
    Jan 4, 2014
Loading...

Share This Page