Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.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.095 X-Spam-Evidence: '*H*': 0.81; '*S*': 0.00; 'all?': 0.09; 'pm,': 0.11; 'wrote:': 0.14; 'lookups': 0.16; 'subject:Pythonic': 0.16; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'away.': 0.23; 'skip:( 30': 0.24; 'creating': 0.26; "i'm": 0.26; 'chris': 0.27; 'message- id:@mail.gmail.com': 0.28; 'subject:?': 0.29; 'fri,': 0.29; "won't": 0.30; 'key,': 0.31; 'does': 0.31; 'paul': 0.32; 'to:addr :python-list': 0.32; 'bit': 0.33; 'right?': 0.35; 'some': 0.37; 'received:209.85': 0.37; 'apr': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'to:addr:python.org': 0.39; 'could': 0.39; 'received:209': 0.39; 'header:Received:5': 0.40; 'might': 0.40; '2011': 0.62; 'making': 0.62; 'evaluate': 0.72; 'received:209.85.210.174': 0.84; 'received:mail- iy0-f174.google.com': 0.84; 'ugly,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=UfObovpqoaLZrewgfSPwF1qk7SK2nYuW8rWNIrDKuh4=; b=qfgfmKi1mqWsfZ1w1rpZWuS41lp7DkHGquXDnVnufX1+3kOtLe45SKXQq025ZtVAkO xuXuRXs/12aJy5X3xhKR2YQjjQpKSy1IsOBhkLga08mSdSrI9YG9jCKnUHeVH+jsw172 yr9q5RTyIEC4/Ustiaz815lMJkOuO5lceot4g= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=TURROB3mAHMtj/4d5k/daLNVtn7d14PJ6fprprUEl5AaeTCWoBxdWf8yfZKHVxQBrM VA5PPx0ZRmJlcvmwPAeMiJu6FKwaBMXRPQsAKeXFAMQjRkQsPcPtHJq575Kp1lwZxnGN ou/lue1hcs/R+ELTOpybN/3JN1hPzULyVKW+Y= MIME-Version: 1.0 In-Reply-To: <7xsjtkx9hy.fsf@ruckus.brouhaha.com> References: <7xsjtkx9hy.fsf@ruckus.brouhaha.com> Date: Fri, 15 Apr 2011 17:35:20 +1000 Subject: Re: Pythonic infinite for loop? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 16 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1302852923 news.xs4all.nl 41117 [::ffff:82.94.164.166]:49281 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3247 On Fri, Apr 15, 2011 at 5:24 PM, Paul Rubin wrote= : > This does 2 lookups per key, which you could avoid by making the code > uglier (untested): > > =A0 sentinel =3D object() > =A0 seq =3D (dct.get('Keyword%d'%i,sentinel) for i in count(1)) > =A0 lst =3D list(takewhile(lambda x: x !=3D sentinel, seq)) If I understand this code correctly, that's creating generators, right? It won't evaluate past the sentinel at all? That might well be what I'm looking for. A bit ugly, but efficient and compact. And I can bury some of the ugliness away. Chris Angelico