Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60711

Re: For-each behavior while modifying a collection

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'element': 0.07; 'removes': 0.07; 'iterate': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:while': 0.09; 'python': 0.11; 'def': 0.12; 'element,': 0.16; 'i.e.,': 0.16; 'iterated': 0.16; 'iteration': 0.16; 'iteration.': 0.16; 'iterator': 0.16; 'loops': 0.16; 'message- id:@post.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'true:': 0.16; 'usable': 0.16; 'elements': 0.16; 'example': 0.22; 'header:User-Agent:1': 0.23; 'entries': 0.24; 'define': 0.26; 'code:': 0.26; 'header:X -Complaints-To:1': 0.27; 'see,': 0.30; 'statement': 0.30; 'received:132': 0.31; 'writes:': 0.31; 'class': 0.32; 'are:': 0.33; 'guess': 0.33; 'not.': 0.33; 'skip:_ 10': 0.34; 'but': 0.35; 'there': 0.35; 'skip:j 20': 0.36; 'yield': 0.36; 'next': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'clear': 0.37; 'performance': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'break': 0.61; 'length': 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'provide': 0.64; 'subject:For': 0.78; '#2:': 0.84; 'protocol,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: For-each behavior while modifying a collection
Date Thu, 28 Nov 2013 17:21:57 +0000 (UTC)
References <CAO5g_efLUD9kJ_W+fBUWv=9yrDggN2560H5Fx3uUVDQ=oAVREg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host sea.gmane.org
User-Agent Loom/3.14 (http://gmane.org/)
X-Loom-IP 132.230.235.10 (Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3365.1385659341.18130.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385659341 news.xs4all.nl 15960 [2001:888:2000:d::a6]:34832
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60711

Show key headers only | View raw


Valentin Zahnd <v.zahnd <at> gmail.com> writes:

> 
> Hello
> 
> For-each does not iterate ober all entries of collection, if one
> removes elements during the iteration.
> 
> Example code:
> 
> def keepByValue(self, key=None, value=[]):
>     for row in self.flows:
>         if not row[key] in value:
>             self.flows.remove(row)
> 
> It is clear why it behaves on that way. Every time one removes an
> element, the length of the colleciton decreases by one while the
> counter of the for each statement is not.
> The questions are:
> 1. Why does the interprete not uses a copy of the collection to
> iterate over it? Are there performance reasons?
> 2. Why is the counter for the iteration not modified?
> 
> Valentin
> 

I guess because it's difficult to generalize this idea.
Python for loops use the iterator protocol, i.e., in order to be usable in a
for loop all an object has to provide is an __iter__ and a __next__ method,
but your suggestion #1 would require it to define a copy method in addition.
As for #2: there is no such thing as a loop counter in a standard for loop,
but it's the iterated object that decides on the next value the loop will
see, and there's no way to move back, e.g.:
class jumplist (list):
    def __iter__(self):
        n=0
        while True:
            if n%3:
                yield self[n]
            else:
                yield None
            n+=1
            if n>=len(self):
                break

jumper=jumplist(range(10))

for element in jumper:
    print(element)

---->
None
1
2
None
4
5
None
7
8
None

Best,
Wolfgang

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: For-each behavior while modifying a collection Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-11-28 17:21 +0000

csiph-web