'while' in list comprehension?

jsaul jsaul at gmx.de
Wed Oct 22 13:59:24 EDT 2003


Hi there,

wouldn't it be useful to have a 'while' conditional in addition to
'if' in list comprehensions?

    foo = []
    for i in bar:
        if len(i) == 0:
            break
        foo.append(i)

would then turn into

    foo = [ i for i in bar while len(i)>0 ]

Is there any reason for not having this kind of thing? I actually
miss it pretty often.

Cheers, jsaul




More information about the Python-list mailing list