[Newby question] List comprehension

Scott David Daniels Scott.Daniels at Acm.Org
Fri Aug 6 16:15:38 CEST 2004


Batista, Facundo wrote:

> [Eelco Hoekema]
> #- def song(f):
> #-     (name, ext) = os.path.splitext(f)
> #-     return ext.lower() == '.mp3'
> files = []
> for (root, dir, files) in os.walk(os.path.abspath('.')):
> 	mp3files = filter(song, files)
> 	if mp3files:
> 		files.append((root, mp3files))
> 
Iwouldn't bother with the funny song construction:
     files = []
     for root, dirs, files in os.walk(os.path.abspath('.')):
         mp3files = [name for name in files
                     if name.lower().endswith('.mp3')]
         if mp3files:
             files.append((root, mp3files))

-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list