Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'modify': 0.07; 'removes': 0.07; 'suppose': 0.07; 'iterate': 0.09; 'pointless': 0.09; 'subject:while': 0.09; 'def': 0.12; '10:49': 0.16; 'collections': 0.16; 'complicated,': 0.16; 'element,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'iterables': 0.16; 'iteration': 0.16; 'iteration.': 0.16; 'iterator': 0.16; 'loops': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'objects.': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'two.': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'written': 0.21; 'example': 0.22; 'header:User-Agent:1': 0.23; 'entries': 0.24; 'references': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'list:': 0.30; 'statement': 0.30; 'implicit': 0.31; 'modified,': 0.31; 'are:': 0.33; 'not.': 0.33; 'copying': 0.34; 'could': 0.34; 'advice': 0.35; 'received:84': 0.35; 'but': 0.35; 'there': 0.35; 'acceptable': 0.36; 'list': 0.37; 'clear': 0.37; 'performance': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'does': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'prices': 0.60; 'most': 0.60; 'length': 0.61; 'new': 0.61; 'simple': 0.61; 'more': 0.64; 'different': 0.65; 'header:Reply- To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'subject:For': 0.78; 'batchelder': 0.84; 'reply-to:addr:python.org': 0.84; 'careful': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=IvYnLtPg c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=OJn0C7AadrgA:10 a=MFmyrltG3qAA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=lXo6rH948BwA:10 a=l4FM98rAXYkYyrUNPJ4A:9 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Thu, 28 Nov 2013 17:25:05 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: For-each behavior while modifying a collection References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385659495 news.xs4all.nl 16001 [2001:888:2000:d::a6]:37973 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60712 On 28/11/2013 17:20, Ned Batchelder wrote: > On 11/28/13 10:49 AM, Valentin Zahnd wrote: >> 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? > > Because implicit copying would be pointless in most cases. Most loops > don't even want to modify the collection, why copy all iterables just in > case your loop might be one of the tiny few that might change the > collection? > > Of course, if that prices is acceptable to you, you could do the copy > yourself: > > for row in list(self.flows): > if row[key] not in value: > self.flows.remove(row) > >> 2. Why is the counter for the iteration not modified? > > Because the list and the iterator over the list are different objects. > I suppose the list and the iterator could have been written to update > when the list is modified, but it could get pretty complicated, even > more so if you want to do the same for other collections like dictionaries. > > The best advice is: don't modify the list, instead make a new list: > > self.flows = [r for r in self.flows if r[key] not in value] > > Be careful though, since there might be other references to the list, > and now you have two. > The simple solution in that case is: self.flows[:] = [r for r in self.flows if r[key] not in value]