'while' in list comprehension?

Skip Montanaro skip at pobox.com
Wed Oct 22 15:12:46 EDT 2003


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

    Emile> How is this different from:

    Emile>      foo = [ i for i in bar if len(i) ]

The first is like:

    _ = []
    for i in bar:
        if not (len(i) > 0):
           break
        _.append(i)
    return _

The second is like:

    _ = []
    for i in bar:
        if len(i) > 0:
            _.append(i)
    return _

Skip





More information about the Python-list mailing list