[os.path.join(r'E:\Python', name) for name in []] returns []

Chris Angelico rosuav at gmail.com
Tue Jan 29 14:33:06 CET 2013


On Wed, Jan 30, 2013 at 12:21 AM, iMath <redstone-cold at 163.com> wrote:
> why [os.path.join(r'E:\Python', name) for name in []] returns [] ?
> please explain it in detail !

That's a list comprehension. If you're familiar with functional
programming, it's like a map operation. Since the input list (near the
end of the comprehension, just inside its square brackets) is empty,
so is the result list, and os.path.join is never called.

I've given you a massive oversimplification. The docs are here:

http://docs.python.org/3.3/tutorial/datastructures.html#list-comprehensions

Pro tip: The docs are there before you ask the question, too. You
might find it faster to search them than to ask and wait for an answer
:)

ChrisA



More information about the Python-list mailing list