Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:both': 0.07; 'logic': 0.09; 'runtime': 0.09; 'def': 0.12; '-tkc': 0.16; 'argument,': 0.16; 'evaluated.': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterable': 0.16; 'iterable:': 0.16; 'nay': 0.16; 'range(0,': 0.16; 'wrote:': 0.18; 'written': 0.21; 'header:In- Reply-To:1': 0.27; 'said,': 0.30; 'statement': 0.30; 'run': 0.32; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'subject:one': 0.36; 'yield': 0.36; 'charset:us-ascii': 0.36; 'form,': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'even': 0.60; 'cost.': 0.60; 'times': 0.62; 'dont': 0.67; 'received:50.22': 0.84; 'canonical': 0.91 Date: Tue, 17 Sep 2013 09:17:44 -0500 From: Tim Chase To: python-list@python.org Subject: Re: Having both if() and for() statements in one liner In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: none 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379427403 news.xs4all.nl 15950 [2001:888:2000:d::a6]:53217 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54302 On 2013-09-17 16:21, Ferrous Cranus wrote: > I just want to say tot he program that > > that only run the for statement if and only if person=='George' > > I dont see nay reason as to why this fails > > perhaps like: > > for times in range(0, 5) if person=='George': > > but that fails too... > there must be written on soem way. The canonical way to do this is the obvious: if person == "George": for times in range(0, 5): ... That said, you can do stupid things to abstract the logic like def iterate_if(condition, iterable): if condition: for item in iterable: yield item which you can use something like for times in iterate_if(person == "George", range(0,5)): ... but I don't advise it. Mainly, because the iterable will be evaluated when passed as an argument, which incurs the runtime cost. In the canonical form, if the test isn't passed, the range(n,m) is never even evaluated. -tkc