Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.056 X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; 'bits': 0.09; 'clean.': 0.09; 'subject:while': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'be:': 0.16; 'bits.': 0.16; 'comp': 0.16; 'enough.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'visible': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'references': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'usually': 0.31; 'are.': 0.31; 'fri,': 0.33; 'actual': 0.34; 'done.': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'should': 0.36; 'list': 0.37; 'step': 0.37; 'nov': 0.38; 'little': 0.38; 'length': 0.61; 'profile': 0.61; "you're": 0.61; "you'll": 0.62; 'more': 0.64; 'between': 0.67; 'importantly,': 0.68; 'subject:For': 0.78; '"fast"': 0.84; 'duplication,': 0.84; 'seldom': 0.84; 'to:none': 0.92; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=u0hkVTNt2YS1tQOcEaoUwJjZrJuHLHkoRY2XkJMZSb0=; b=SWD4mGXNa3W26+KEPodWAQZPAgrYOsWd17/fNq4NyKHfHvDMI97ARsBzHv3ASehG4U m5mDg/tExS8FxLOsbGkK3uZ8gm3s0Pvrd28dQRvU2p5mT7uIBB5RtgAWTmYdjogNnXFA PzzsLyRrgm5Os9I++9XNrED9X+97LbfsN2ZO4XAeE/lEcgdqjZNyfRbc79MTtyJ96LJI 0uvADe8RHrk2M14i+7NAisNKkRm4raCl5Eds5kGjiENOePYNEzGHtxQHITIZVq8poH4+ aFC5KBtclNNTdwQNdPTDrET0V9/KtTPOV5iDYDZvhPfaPrTlAl7V8NiDbjzmkSIOwmQO 3bQQ== MIME-Version: 1.0 X-Received: by 10.68.225.9 with SMTP id rg9mr15328643pbc.122.1385718473482; Fri, 29 Nov 2013 01:47:53 -0800 (PST) In-Reply-To: References: <52977C71.4010200@mrabarnett.plus.com> Date: Fri, 29 Nov 2013 20:47:53 +1100 Subject: Re: For-each behavior while modifying a collection From: Chris Angelico Cc: python-list@python.org Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385718481 news.xs4all.nl 15908 [2001:888:2000:d::a6]:60697 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60761 On Fri, Nov 29, 2013 at 9:14 AM, Valentin Zahnd wrote: > def keepByValue(self, key=None, value=[]): > tmpFlows = [] > while len(self.flows) > 0: > row = self.flows.pop() > if row[key] in value: > tmpFlows.append(row) > self.flows = tmpFlows This is almost certainly going to be less efficient than the comprehension - it churns the length of the list terribly. There's very little duplication, as the actual _contents_ will not be duplicated, only references to them; so the list comp is both clean and efficient. More importantly, the way you write a Python program should be: 1) Make it work and look clean. 2) See if it's fast enough. If so - and it usually will be - you're done. 3) Profile it and find out where the slow bits really are. 4) Try to speed up some of the slow bits. You'll seldom get past step 2. The difference between "fast" and "fast enough" is usually not visible to a human. ChrisA