loops -> list/generator comprehensions

Steven Bethard steven.bethard at gmail.com
Sun Feb 6 16:00:49 EST 2005


On 6 Feb 2005 11:28:37 -0800, <jamesthiele.usenet at gmail.com> wrote:
 >
 >     walkList  = [(x[0], x[2]) for x in os.walk(".")]
 >     filenames = []
 >     for dir, files in walkList:
 >         filenames.extend(["/".join([dir, f]) for f in files])

Caleb Hattingh top-posted:
> I would be interested to see an example of code that is more concise 
> but  yet as *clear* as the one you already have.

Out of curiosity, do you find:

filenames = [os.path.join(dirpath, filename)
              for dirpath, _, filenames in os.walk('.')
              for filename in filenames]

harder to read?

Steve



More information about the Python-list mailing list