help with simple script and strings

Lloyd Kvam python at venix.com
Mon Apr 14 00:11:47 UTC 2008


On Sun, 2008-04-13 at 17:05 -0400, seth vidal wrote:
> > newDirectories = []
> > for directory in directories:
> >      if directory.startswith('200'):
> >          newDirectories.append(directory)
> > directories = newDirectories
> > 
> > If you want speed and code compactness use a list comprehension.
> > 
> 
> And just as another note - you better be doing this several hundred
> thousand times to justify the complete unreadability of list
> comprehensions.

directories = [directory
    for directory in directories
    if directory.startswith('200')
    ]

The change in line ordering is unsettling at first.  But I think the
intent is very clear with a list comprehension.  Certainly the phrasing
is quite similar.

-- 
Lloyd Kvam
Venix Corp
DLSLUG/GNHLUG library
http://www.librarything.com/catalog/dlslug
http://www.librarything.com/profile/dlslug
http://www.librarything.com/rsshtml/recent/dlslug




More information about the python-devel mailing list