Bug in list comprehensions?

Discussion in 'Python' started by Iain King, Jun 7, 2006.

  1. Iain King

    Iain King Guest

    I was playing with list comprehensions, to try and work out how doubled
    up versions work (like this one from another thread: [i for i in
    range(9) for j in range(i)]). I think I've figured that out, but I
    found something strange along the way:

    >>> alpha = ["one", "two", "three"]
    >>> beta = ["A", "B", "C"]
    >>> [x for x in alpha for y in beta]

    ['one', 'one', 'one', 'two', 'two', 'two', 'three', 'three', 'three']
    >>> [x for x in y for y in beta]

    ['C', 'C', 'C']
    >>> beta = [alpha, alpha, alpha]
    >>> beta

    [['one', 'two', 'three'], ['one', 'two', 'three'], ['one', 'two',
    'three']]
    >>> [x for x in y for y in beta]

    ['C', 'C', 'C']
    >>> [y for y in beta]

    [['one', 'two', 'three'], ['one', 'two', 'three'], ['one', 'two',
    'three']]
    >>> [x for x in y for y in beta]

    ['one', 'one', 'one', 'two', 'two', 'two', 'three', 'three', 'three']

    Shoudn't both lines '[x for x in y for y in beta]' produce the same
    list?
    I'm guessing I'm the one confused here... but I'm confused! What's
    going on?

    Iain
    Iain King, Jun 7, 2006
    #1
    1. Advertising

  2. Iain King wrote:

    > I'm guessing I'm the one confused here... but I'm confused! What's
    > going on?


    reading the documentation may help:

    /.../ the elements of the new list are those that would be produced
    by considering each of the for or if clauses a block, nesting from left
    to right, and evaluating the expression to produce a list element each
    time the innermost block is reached.

    the clauses nest from left to right, not from right to left, so "[x for
    x in y for y in beta]" is equivalent to

    out = []
    for x in y:
    for y in beta:
    out.append(x)

    </F>
    Fredrik Lundh, Jun 7, 2006
    #2
    1. Advertising

  3. Iain King

    Duncan Booth Guest

    Iain King wrote:

    >>>> [x for x in y for y in beta]

    > ['C', 'C', 'C']
    >>>> [y for y in beta]

    > [['one', 'two', 'three'], ['one', 'two', 'three'], ['one', 'two',
    > 'three']]
    >>>> [x for x in y for y in beta]

    > ['one', 'one', 'one', 'two', 'two', 'two', 'three', 'three', 'three']
    >
    > Shoudn't both lines '[x for x in y for y in beta]' produce the same
    > list?


    [x for x in y for y in beta] is a shorthand for:

    tmp = []
    for x in y:
    for y in beta:
    tmp.append(x)

    So x iterates over whatever y is before the loop starts, and y iterates
    over beta (but that doesn't affect what x is iterating over).

    The important thing is to remember that the order of 'for' and 'if'
    statements is the same as though you had written the for loop out in full.
    Duncan Booth, Jun 7, 2006
    #3
  4. Fredrik Lundh <> wrote:
    >Iain King wrote:
    >> I'm guessing I'm the one confused here... but I'm confused! What's
    >> going on?

    >the clauses nest from left to right, not from right to left, so "[x for
    >x in y for y in beta]" is equivalent to
    >
    > out = []
    > for x in y:
    > for y in beta:
    > out.append(x)


    And a list comprehension doesn't get a namespace to itself (cf.
    generator comprehensions) so "leaks" its variables. Exactly as
    above. So the y being iterated over in "for x in y" is the y
    from the previous inner iteration ("for y in beta").

    --
    \S -- -- http://www.chaos.org.uk/~sion/
    ___ | "Frankly I have no feelings towards penguins one way or the other"
    \X/ | -- Arthur C. Clarke
    her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
    Sion Arrowsmith, Jun 7, 2006
    #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. seguso
    Replies:
    9
    Views:
    377
    seguso
    Dec 22, 2004
  2. Dave Kuhlman

    Re: Style in list comprehensions

    Dave Kuhlman, Aug 15, 2003, in forum: Python
    Replies:
    1
    Views:
    323
    Alex Martelli
    Aug 16, 2003
  3. Frank Millman

    Simple db using list comprehensions

    Frank Millman, Apr 5, 2004, in forum: Python
    Replies:
    1
    Views:
    277
    Paddy McCarthy
    Apr 16, 2004
  4. Elaine Jackson

    list comprehensions

    Elaine Jackson, Apr 7, 2004, in forum: Python
    Replies:
    10
    Views:
    597
  5. Steven Bethard
    Replies:
    7
    Views:
    387
    Rocco Moretti
    Jan 20, 2006
Loading...

Share This Page