Pythonic infinite for loop?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Apr 15 08:52:32 EDT 2011


On Fri, 15 Apr 2011 13:58:22 +1000, Chris Angelico wrote:

> The dictionary is potentially a lot larger than this particular set of
> values (it's a mapping of header:value for a row of a user-provided CSV
> file). Does this make a difference to the best option? (Currently I'm
> looking at "likely" figures of 60+ keys in the dictionary and 3-8
> postage options to pick up, but both of those could increase
> significantly before production.)

SIXTY keys?

When you get to sixty thousand keys, it might take a few seconds to 
process.


> Efficiency is important, though not king; this whole code will be inside
> a loop. But readability is important too.
> 
> I can't just give all of dct.values() to parse_kwdlist; the function
> parses a particular format of text string, and it's entirely possible
> that other values would match that format (some of them are pure
> free-form text). This has to get only the ones starting with Keyword,
> and in order. Steven, the line you suggested:
> 
> lst = [parse_kwdlist(dct["Keyword%d"%i]) for i in xrange(1, len(dct)+1)]
> 
> will bomb with KeyError when it hits the first one that isn't present, I
> assume. Is there an easy way to say "and when you reach any exception,
> not just StopIteration, return the list"? (I could wrap the whole "lst =
> " in a try/except, but then it won't set lst at all.)

No. 

You could use the list comprehension form at the cost of running over the 
dict twice:

maxkey = 0
for key in dct:
    if key.startswith("Keyword"):
        maxkey = max(maxkey, int(key[7:]))
lst = [parse_kwdlist(dct["Keyword%d"%i]) for i in xrange(1, maxkey+1)]


but quite frankly, at this point I'd say, forget the one-liner, do it the 
old-fashioned way with a loop. Or change your data structure: often you 
can simplify a task drastically just by changing the way you store the 
data.




-- 
Steven



More information about the Python-list mailing list