Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'processing.': 0.03; 'dictionary': 0.07; 'numeric': 0.07; 'python': 0.07; 'pm,': 0.11; 'output': 0.12; 'advance': 0.14; 'wrote:': 0.14; '"hey': 0.16; '9:10': 0.16; 'adam': 0.16; 'appends': 0.16; 'begging': 0.16; 'keyerror:': 0.16; 'pythonic': 0.16; 'subject:Pythonic': 0.16; 'top,': 0.16; 'xrange': 0.16; 'things.': 0.16; 'input': 0.18; 'wondering': 0.19; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'loop': 0.22; 'thu,': 0.22; "i'm": 0.26; 'chris': 0.27; 'object': 0.27; 'url:mailman': 0.27; 'function': 0.27; 'message- id:@mail.gmail.com': 0.28; 'looks': 0.28; 'thanks': 0.29; 'string': 0.29; 'subject:?': 0.29; 'elements': 0.29; 'forgot': 0.29; 'probably': 0.30; '(the': 0.30; 'list': 0.30; 'this.': 0.30; 'cc:addr:python.org': 0.31; 'one,': 0.31; 'turned': 0.31; 'construct': 0.31; 'skip:\xa0 40': 0.31; 'value)': 0.31; "can't": 0.31; 'called': 0.32; 'url:listinfo': 0.33; 'beginning': 0.33; 'break': 0.33; 'generally': 0.33; 'there': 0.35; 'question': 0.35; 'apologies': 0.35; 'like:': 0.35; 'try:': 0.35; 'doing': 0.36; 'two': 0.37; 'some': 0.37; 'received:209.85': 0.37; 'url:python': 0.37; 'apr': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'url:org': 0.38; 'draft': 0.39; 'comes': 0.39; 'received:209': 0.39; 'how': 0.39; 'except': 0.39; 'takes': 0.40; 'header:Received:5': 0.40; '2011': 0.62; 'become': 0.70; 'vital': 0.77; 'received:209.85.210.174': 0.84; 'received:mail- iy0-f174.google.com': 0.84; 'ask.': 0.91 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:cc:content-type :content-transfer-encoding; bh=53H5FcHbQZTQzwJKFeFy8uMl4LJnWfNgWMtOi8e0INk=; b=Nf0d14owCM3L/FmOT1saYj2YR9Fkb2YWshhk5gyb3QlXTzJPmoEH2WbZwkksBFBeMq WItrr1tu6AN27ThvhL/98w4qJ+vasPXvQEW5X2P3dHU9XMyMeKtxidh4WmZ+ohH8B7hb yf3Tm25Imtxas4C+/G03KL+FrDZy3IMeN8+cg= 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 :cc:content-type:content-transfer-encoding; b=AzcaMno+VqTSm+74PODYKk0mwP1Fgfz8sxUbj3VRRt9lTQ+EtIuBG4mVkp9jjgt7mc P+zYMfRH7kUmt5yCp8JIt/VaWZO5k9mFNJRYkGPAt4QT8etLyUt0Xc563z9t0blLUTE+ S5vqNGFkf2Ubs692kRL7deGfv0iP6mGWvdmJ4= MIME-Version: 1.0 In-Reply-To: References: Date: Thu, 14 Apr 2011 21:41:19 -0500 Subject: Re: Pythonic infinite for loop? From: John Connor To: Chris Angelico Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 51 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1302835283 news.xs4all.nl 41117 [::ffff:82.94.164.166]:51721 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3238 If I understand your question correctly, what you want is probably something like: i =3D 0 lst=3D[] while True: try: lst.append(parse_kwdlist(dct["Keyword%d"%i])) i +=3D 1 except KeyError: break --jac On Thu, Apr 14, 2011 at 9:10 PM, Chris Angelico wrote: > Apologies for interrupting the vital off-topic discussion, but I have > a real Python question to ask. > > I'm doing something that needs to scan a dictionary for elements that > have a particular beginning and a numeric tail, and turn them into a > single list with some processing. I have a function parse_kwdlist() > which takes a string (the dictionary's value) and returns the content > I want out of it, so I'm wondering what the most efficient and > Pythonic way to do this is. > > My first draft looks something like this. The input dictionary is > called dct, the output list is lst. > > lst=3D[] > for i in xrange(1,10000000): # arbitrary top, don't like this > =A0try: > =A0 =A0lst.append(parse_kwdlist(dct["Keyword%d"%i])) > =A0except KeyError: > =A0 =A0break > > I'm wondering two things. One, is there a way to make an xrange object > and leave the top off? (Sounds like I'm risking the numbers > evaporating or something.) And two, can the entire thing be turned > into a list comprehension or something? Generally any construct with a > for loop that appends to a list is begging to become a list comp, but > I can't see how to do that when the input comes from a dictionary. > > In the words of Adam Savage: "Am I about to feel really, really stupid?" > > Thanks in advance for help... even if it is just "hey you idiot, you > forgot about X"! > > Chris Angelico > -- > http://mail.python.org/mailman/listinfo/python-list >