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

Discussion in 'Python' started by iMath, Jan 29, 2013.

  1. iMath

    iMath Guest

    why [os.path.join(r'E:\Python', name) for name in []] returns [] ?
    please explain it in detail ï¼
     
    iMath, Jan 29, 2013
    #1
    1. Advertising

  2. iMath

    iMath Guest

    在 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 []]

    []
     
    iMath, Jan 29, 2013
    #2
    1. Advertising

  3. On Wed, Jan 30, 2013 at 12:21 AM, iMath <> 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
     
    Chris Angelico, Jan 29, 2013
    #3
  4. 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
     
    Steven D'Aprano, Jan 29, 2013
    #4
  5. iMath

    Dave Angel Guest

    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
     
    Dave Angel, Jan 29, 2013
    #5
  6. iMath

    rusi Guest

    Re: ] returns []

    On Jan 29, 6:22 pm, iMath <> 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.
     
    rusi, Jan 29, 2013
    #6
  7. iMath

    Guest

    Re: ] returns []

    MUSATOV
     
    , Jan 30, 2013
    #7
  8. iMath

    iMath Guest

    在 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 .
     
    iMath, Jan 30, 2013
    #8
  9. iMath

    iMath Guest

    在 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 !
     
    iMath, Jan 30, 2013
    #9
  10. On Wed, Jan 30, 2013 at 12:56 PM, iMath <> 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
     
    Chris Angelico, Jan 30, 2013
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    898
    Alan Silver
    Jun 5, 2006
  2. googleboy
    Replies:
    1
    Views:
    936
    Benji York
    Oct 1, 2005
  3. Steve
    Replies:
    1
    Views:
    984
    Fredrik Lundh
    Dec 13, 2005
  4. Jean-Michel Pichavant
    Replies:
    0
    Views:
    139
    Jean-Michel Pichavant
    Jan 29, 2013
  5. Chris Angelico

    Re: [Python-ideas] os.path.join

    Chris Angelico, Nov 4, 2013, in forum: Python
    Replies:
    0
    Views:
    107
    Chris Angelico
    Nov 4, 2013
Loading...

Share This Page