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


Groups > comp.lang.python > #60697

For-each behavior while modifying a collection

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <v.zahnd@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.017
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'removes': 0.07; 'iterate': 0.09; 'subject:while': 0.09; 'def': 0.12; 'element,': 0.16; 'iteration': 0.16; 'iteration.': 0.16; 'elements': 0.16; 'example': 0.22; 'entries': 0.24; 'code:': 0.26; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'are:': 0.33; 'not.': 0.33; 'received:google.com': 0.35; 'there': 0.35; 'clear': 0.37; 'performance': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'length': 0.61; 'subject:For': 0.78
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=THYGxAkElHbxgIL3XWx2qt42e/4Y85AXEY4F0EQKEtI=; b=XN3bk9va8ra8dKaFU1GpflQxHaPeDIUWhdRO1cJunDPZ5hZYAw7/Ae7tMQXCyL2+Ib s05toEJVnzcrfEAnLodaZabBo/GbJEo5qhTMYPchoKrAIaWldl8RMn9ZsuVgFZ+CU7Az 7Z7bvKsoGLvNuSqGW2SzX3qGKKwQdqSHLZaSVnDyWwfO399qaEzSfJqlWeLYKHRFZW8K aGxQU9OZbJXTVDk2ZRbhM6lkPXM4B/0c/j656Qe84bdybjzkAj1rx+XIPPmApXaA3/8o TIfQwcnyDteRmyAC++6YpeIExuAo3kejZdESTnJFA70lyfVkVn5Ub3n3XC0Z+pyw8Pxo Gogw==
MIME-Version 1.0
X-Received by 10.220.47.10 with SMTP id l10mr1388724vcf.32.1385653761270; Thu, 28 Nov 2013 07:49:21 -0800 (PST)
Date Thu, 28 Nov 2013 16:49:21 +0100
Subject For-each behavior while modifying a collection
From Valentin Zahnd <v.zahnd@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-Mailman-Approved-At Thu, 28 Nov 2013 16:50:57 +0100
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.3360.1385653859.18130.python-list@python.org> (permalink)
Lines 21
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385653859 news.xs4all.nl 15895 [2001:888:2000:d::a6]:47404
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60697

Show key headers only | View raw


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

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


Thread

For-each behavior while modifying a collection Valentin Zahnd <v.zahnd@gmail.com> - 2013-11-28 16:49 +0100
  Re: For-each behavior while modifying a collection Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-29 03:22 +0000
    Re: For-each behavior while modifying a collection Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-29 11:37 -0500

csiph-web