Pythonic infinite for loop?

Roy Smith roy at panix.com
Fri Apr 15 15:13:31 CEST 2011


In article <4da83f8f$0$29986$c3e8da3$5496439d at news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:

> for key in dct:
>     if key.startswith("Keyword"):
>         maxkey = max(maxkey, int(key[7:]))

I would make that a little easier to read, and less prone to "Did I 
count correctly?" bugs with something like:

prefix = "Keyword"
n = len(prefix)
for key in dct:
    name, value = key[:n], key[n:]
    if name == prefix:
        maxkey = max(maxkey, int(value))



More information about the Python-list mailing list