execution order in list/generator expression

Discussion in 'Python' started by bonono@gmail.com, Oct 23, 2005.

  1. Guest

    Hi,

    I am wondering how this is evaluated.

    a=(x for x in [1,2,3,4])
    p=[4,5]

    c=[x for x in p if x in list(a)]

    c is []

    but if I expand a first, like a = list(a)

    c is [4]

    So it seems that the "if" part don't get expanded ?
     
    , Oct 23, 2005
    #1
    1. Advertising

  2. Devan L Guest

    wrote:
    > Hi,
    >
    > I am wondering how this is evaluated.
    >
    > a=(x for x in [1,2,3,4])
    > p=[4,5]
    >
    > c=[x for x in p if x in list(a)]
    >
    > c is []
    >
    > but if I expand a first, like a = list(a)
    >
    > c is [4]
    >
    > So it seems that the "if" part don't get expanded ?


    Well, for every element in p it recalculates list(a). Since the
    generator is exhausted after making a list from it, it gives you
    nothing afterwards. So after it checks the first element, it's
    equivalent to [x for x in p if x in []].
     
    Devan L, Oct 23, 2005
    #2
    1. Advertising

  3. Robert Kern Guest

    wrote:
    > Hi,
    >
    > I am wondering how this is evaluated.
    >
    > a=(x for x in [1,2,3,4])
    > p=[4,5]
    >
    > c=[x for x in p if x in list(a)]
    >
    > c is []


    No it isn't.

    In [1]: a=(x for x in [1,2,3,4])

    In [2]: p=[4,5]

    In [3]: c=[x for x in p if x in list(a)]

    In [4]: c
    Out[4]: [4]

    I'm willing to bet that you used up the 'a' iterator before you ran that
    list comprehension, though.

    In [5]: c=[x for x in p if x in list(a)]

    In [6]: c
    Out[6]: []

    Note that "x in list(a)" gets executed on each iteration, but the
    iterator is used up on the first time.

    In [7]: a=(x for x in [1,2,3,4])

    In [8]: p = [4, 5, 2, 3]

    In [9]: c=[x for x in p if x in list(a)]

    In [10]: c
    Out[10]: [4]

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Oct 23, 2005
    #3
  4. Guest

    Ah, no wonder. I test with p=[5,4].

    thanks. so basically, I still need to expand it first given this
    behaviour.

    Robert Kern wrote:
    > wrote:
    > > Hi,
    > >
    > > I am wondering how this is evaluated.
    > >
    > > a=(x for x in [1,2,3,4])
    > > p=[4,5]
    > >
    > > c=[x for x in p if x in list(a)]
    > >
    > > c is []

    >
    > No it isn't.
    >
    > In [1]: a=(x for x in [1,2,3,4])
    >
    > In [2]: p=[4,5]
    >
    > In [3]: c=[x for x in p if x in list(a)]
    >
    > In [4]: c
    > Out[4]: [4]
    >
    > I'm willing to bet that you used up the 'a' iterator before you ran that
    > list comprehension, though.
    >
    > In [5]: c=[x for x in p if x in list(a)]
    >
    > In [6]: c
    > Out[6]: []
    >
    > Note that "x in list(a)" gets executed on each iteration, but the
    > iterator is used up on the first time.
    >
    > In [7]: a=(x for x in [1,2,3,4])
    >
    > In [8]: p = [4, 5, 2, 3]
    >
    > In [9]: c=[x for x in p if x in list(a)]
    >
    > In [10]: c
    > Out[10]: [4]
    >
    > --
    > Robert Kern
    >
    >
    > "In the fields of hell where the grass grows high
    > Are the graves of dreams allowed to die."
    > -- Richard Harter
     
    , Oct 23, 2005
    #4
    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. Martin Maurer
    Replies:
    3
    Views:
    4,875
    Peter
    Apr 19, 2006
  2. snacktime
    Replies:
    13
    Views:
    567
    Dan Sommers
    Feb 22, 2005
  3. Replies:
    9
    Views:
    548
  4. -heidelberg.de
    Replies:
    2
    Views:
    283
    James Kanze
    Jan 11, 2008
  5. candide
    Replies:
    3
    Views:
    394
    candide
    Sep 20, 2009
Loading...

Share This Page