Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [os.path.join(r'E:\Python', name) for name in []] returns []

Reply
Thread Tools

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

 
 
iMath
Guest
Posts: n/a
 
      01-29-2013
why [os.path.join(r'E:\Python', name) for name in []] returns [] ?
please explain it in detail !
 
Reply With Quote
 
 
 
 
iMath
Guest
Posts: n/a
 
      01-29-2013
在 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 []]

[]
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      01-29-2013
On Wed, Jan 30, 2013 at 12:21 AM, iMath <(E-Mail Removed)> 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/...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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-29-2013
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

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      01-29-2013
On 01/29/2013 08:21 AM, iMath wrote:
> 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.

--
DaveA
 
Reply With Quote
 
rusi
Guest
Posts: n/a
 
      01-29-2013
On Jan 29, 6:22*pm, iMath <(E-Mail Removed)> wrote:
> 在 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.
 
Reply With Quote
 
marty.musatov@gmail.com
Guest
Posts: n/a
 
      01-30-2013
MUSATOV
 
Reply With Quote
 
iMath
Guest
Posts: n/a
 
      01-30-2013
在 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 .
 
Reply With Quote
 
iMath
Guest
Posts: n/a
 
      01-30-2013
在 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 !
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      01-30-2013
On Wed, Jan 30, 2013 at 12:56 PM, iMath <(E-Mail Removed)> wrote:
> 在 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [os.path.join(r'E:\Python', name) for name in []] returns [] Jean-Michel Pichavant Python 0 01-29-2013 01:33 PM
user.identity.name returns old AD user name Antonio O''Neal ASP .Net Security 5 11-03-2008 12:27 PM
Sorted Returns List and Reversed Returns Iterator ++imanshu Python 7 08-23-2008 04:25 AM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
block returns and hash element returns Trans Ruby 2 11-06-2005 12:15 PM



Advertisments