Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed0.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.83.MISMATCH!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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'elegant': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'ifilter': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'say.': 0.16; 'wrote:': 0.18; 'meant': 0.20; 'solution.': 0.20; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'possibly': 0.26; 'header:X-Complaints-To:1': 0.27; 'writes:': 0.31; 'this.': 0.32; 'there': 0.35; 'subject:?': 0.36; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'subject:more': 0.64; 'more': 0.64; 'subject:there': 0.68; 'repeat': 0.74; 'subject:this': 0.83; 'received:139': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Neal Becker Subject: Re: Is there a more elegant way to spell this? Date: Tue, 27 Jan 2015 13:25:54 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: exa2-in-fw-01-epn.hns.com User-Agent: KNode/4.14.3 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422383168 news.xs4all.nl 2886 [2001:888:2000:d::a6]:51672 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84662 Jussi Piitulainen wrote: > Neal Becker writes: > >> Is there a more elegant way to spell this? >> >> for x in [_ for _ in seq if some_predicate]: > > If you mean some_predicate(_), then possibly this. > > for x in filter(some_predicate, seq): > handle(x) > > If you mean literally some_predicate, then perhaps this. > > if some_predicate: > for x in seq: > handle(x) > > Unless you also have in mind an interesting arrangement where > some_predicate might change during the loop, like this. > > for x in [_ for _ in seq if some_predicate]: > ... > some_predicate = fubar(x) > ... > > Then I have nothing to say. To clarify, I meant some_predicate(_), and then ifilter looks like a nice solution. -- -- Those who don't understand recursion are doomed to repeat it