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

I

iMath

why [os.path.join(r'E:\Python', name) for name in []] returns [] ?
please explain it in detail ï¼
 
C

Chris Angelico

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
 
S

Steven D'Aprano

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

Because you are iterating over an empty list, [].

That list comprehension is the equivalent of:


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


Since you iterate over an empty list, the body of the loop never executes,
and the result list remains empty.

What did you expect it to do?
 
D

Dave Angel

why [os.path.join(r'E:\Python', name) for name in []] returns [] ?
please explain it in detail ï¼

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

It'd be nice if you would explain what part of it bothers you. Do you
know what a list comprehension is? Do you know how to decompose a list
comprehension into a for-loop? Do you know that [] is an empty list object?


res = []
for name in []:
res.append( XXXX )

Since the for loop doesn't loop even once, the result is the initial
value, the empty loop.
 
R

rusi

在 2013å¹´1月29日星期二UTC+8下åˆ9æ—¶21分16秒,iMath写é“:
why [os.path.join(r'E:\Python', name) for name in []] returns [] ? please explain it in detail ï¼
[os.path.join(r'E:\Python', name) for name in []]

[]

[Small algebra lesson]
In algebra there is the concept of identity and absorbent.
For example, 1 is the identity for multiply and 0 is the absorbent.
ie for all x: 1 * x = x
and 0 * x = 0
[end algebra lesson]

In the case of lists, [] is an identity for ++ but behaves like an
absorbent for comprehensions.
Modern terminology for 'absorbent' is 'zero-element'. I personally
find the older terminology more useful.

Others have pointed out why operationally [] behaves like an absorbent
in comprehensions.
Ive seen even experienced programmers trip up on this so I believe its
good to know it as an algebraic law in addition to the operational
explanation.
 
I

iMath

在 2013å¹´1月29日星期二UTC+8下åˆ9æ—¶33分26秒,Steven D'Aprano写é“:
iMath wrote: > why [os.path.join(r'E:\Python', name) for name in []] returns [] ? Because you are iterating over an empty list, []. That list comprehension is the equivalent of: result = [] for name in []: result.append( os.path.join(r'E:\Python', name) ) Since you iterate over an empty list, the body of the loop never executes, and the result list remains empty. What did you expect it to do? -- Steven

just in order to get the full path name of each file .
 
I

iMath

在 2013å¹´1月29日星期二UTC+8下åˆ9æ—¶21分16秒,iMath写é“:
why [os.path.join(r'E:\Python', name) for name in []] returns [] ? please explain it in detail ï¼

thanks you all ,now I have a good understanding of it !
 
C

Chris Angelico

在 2013å¹´1月29日星期二UTC+8下åˆ9æ—¶33分26秒,Steven D'Aprano写é“:
iMath wrote: > why [os.path.join(r'E:\Python', name) for name in []] returns [] ? Because you are iterating over an empty list, []. That list comprehension is the equivalent of: result = [] for name in []: result.append(os.path.join(r'E:\Python', name) ) Since you iterate over an empty list, the body of the loop never executes, and the result list remains empty. Whatdid you expect it to do? -- Steven

just in order to get the full path name of each file .

Then it's done exactly what it should. It's given you the full path of
all of your list of zero files.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top