loops -> list/generator comprehensions

Caleb Hattingh caleb1 at telkomsa.net
Sun Feb 6 22:47:04 EST 2005


I would be interested to see an example of code that is more concise but  
yet as *clear* as the one you already have.  I can actually read and  
understand what you've got there.   That's cool :)


On 6 Feb 2005 11:28:37 -0800, <jamesthiele.usenet at gmail.com> wrote:

> I wrote this little piece of code to get a list of relative paths of
> all files in or below the current directory (*NIX):
>
>     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])
>
> It works fine, I don't need to change it, but I know there is a one
> liner list/generator comprehension to do this - I'm just not well
> enough versed in comprehensions to figure it out. Can someone please
> show me what it is?
>
> Even better, is there a generalized way to transform simple loops into
> comprehensions that someone can point me to?
>
> james
>




More information about the Python-list mailing list