question regarding list comprehensions

Diez B. Roggisch deets at nospam.web.de
Mon Oct 20 10:24:26 EDT 2008


Pat wrote:

> I have written chunks of Python code that look this:
> 
>      new_array = []
>      for a in array:
>          if not len( a ):
>              continue
>          new_array.append( a )

new_array = [a for a in array if len(a)]
 
> and...
> 
>      string = ""
>      for r in results:
>          if not r.startswith( '#' ):
>              string =+ r


"".join(r for r in results if not r.startswith("#"))

Diez



More information about the Python-list mailing list