Bug with lists of pairs of lists and append()

Discussion in 'Python' started by Gabriel Zachmann, Sep 28, 2007.

  1. Well,

    could some kind soul please explain to me why the following trivial code
    is misbehaving?


    #!/usr/bin/python
    s = []
    l = [ 0 ]
    r = [0, 0]
    while r:
    x = (l,r)
    print x
    s.append( x )
    l.append( r.pop(0) )
    print s



    The output I get is:

    ([0], [0, 0])
    ([0, 0], [0])
    [([0, 0, 0], []), ([0, 0, 0], [])]

    and the error is in the last line: the two pairs in the outer list are
    identical and they should be equal to the pairs one the first and the
    2nd line, respectively! Shouldn't they?

    I think I'm going nuts -- for the life of me I don't see what's going on ...

    Thanks a lot in advance for any insights, etc.

    Best regards,
    Gabriel.
    Gabriel Zachmann, Sep 28, 2007
    #1
    1. Advertising

  2. Gabriel Zachmann

    TeroV Guest

    Gabriel Zachmann wrote:
    > Well,
    >
    > could some kind soul please explain to me why the following trivial code
    > is misbehaving?
    >
    >
    > #!/usr/bin/python
    > s = []
    > l = [ 0 ]
    > r = [0, 0]
    > while r:
    > x = (l,r)
    > print x
    > s.append( x )
    > l.append( r.pop(0) )
    > print s
    >
    >
    >
    > The output I get is:
    >
    > ([0], [0, 0])
    > ([0, 0], [0])
    > [([0, 0, 0], []), ([0, 0, 0], [])]
    >
    > and the error is in the last line: the two pairs in the outer list are
    > identical and they should be equal to the pairs one the first and the
    > 2nd line, respectively! Shouldn't they?
    >
    > I think I'm going nuts -- for the life of me I don't see what's going on
    > ...
    >
    > Thanks a lot in advance for any insights, etc.
    >
    > Best regards,
    > Gabriel.


    You didn't say what it is supposed to do.
    But, does replacing line "x = (l, r)" with "x = l[:], r[:]" do the trick?

    In the original code you do basically the same as this
    >>> a = []
    >>> b = [1,2]
    >>> a.append(b)
    >>> b.append(3)
    >>> a

    [[1, 2, 3]]

    HTH :)
    TeroV, Sep 28, 2007
    #2
    1. Advertising

  3. Thanks a lot for your response, too.
    Best regards,
    Gabriel.

    --
    ______________________________________________________________
    Life is so constructed that the event does not, cannot,
    will not match the expectation. (Charlotte Bronte)
    ______________________________________________________________
    zach in.tu-clausthal.de __@/' www.gabrielzachmann.org
    ______________________________________________________________
    Gabriel Zachmann, Oct 1, 2007
    #3
    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. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    378
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  2. Piotr Filip Mieszkowski

    Pairs, lists and a recursive type

    Piotr Filip Mieszkowski, Oct 13, 2005, in forum: C++
    Replies:
    4
    Views:
    389
    Piotr Filip Mieszkowski
    Oct 19, 2005
  3. rhXX
    Replies:
    7
    Views:
    224
    Terry Reedy
    Jun 7, 2007
  4. HYRY
    Replies:
    10
    Views:
    575
    Bruno Desthuilliers
    Sep 26, 2007
  5. Gabriel Zachmann

    Bug with lists of pairs of lists and append()

    Gabriel Zachmann, Sep 28, 2007, in forum: Python
    Replies:
    5
    Views:
    252
    Gabriel Zachmann
    Oct 1, 2007
Loading...

Share This Page