#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Appending two lists together?


    Hey guys,

    We use sage to code with python, and I am not sure if it's sage or if it's just python, but what I want to do is:

    list = [[0, 0], [0, 0], [0, 1], [0, 1]]
    list[0].append(0)

    and I want for the list to now be:

    [[0,0,0], [0,0], [0,1], [0,1]]

    But it's doing this:
    [[0,0,0], [0,0,0], [0,1], [0,1]]

    Any help with this??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    On Python 2.6.5 (but other versions are not likely to differ):
    Code:
    >>> list = [[0,0], [0,0], [0,1], [0,1]]
    >>> list[0].append(0)
    >>> list
    [[0, 0, 0], [0, 0], [0, 1], [0, 1]]
    Please post an actual code example that shows the behavior in your post.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Doesn't look like the problem involves Macsyma, but it might.

    You probably created your original list equivalently thus:
    Code:
    >>> ZZ=[0,0]
    >>> ZO=[0,1]
    >>> LIST = [ZZ,ZZ,ZO,ZO]
    >>> LIST
    [[0, 0], [0, 0], [0, 1], [0, 1]]
    >>> LIST[0].append('references the same object')
    >>> LIST
    [[0, 0, 'references the same object'], [0, 0, 'references the same object'], [0, 1], [0, 1]]
    >>>
    The recent posts in this thread contain expanations.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Doesn't look like the problem involves Macsyma, but it might.

    You probably created your original list equivalently thus:
    Code:
    >>> ZZ=[0,0]
    >>> ZO=[0,1]
    >>> LIST = [ZZ,ZZ,ZO,ZO]
    >>> LIST
    [[0, 0], [0, 0], [0, 1], [0, 1]]
    >>> LIST[0].append('references the same object')
    >>> LIST
    [[0, 0, 'references the same object'], [0, 0, 'references the same object'], [0, 1], [0, 1]]
    >>>
    The recent posts in this thread contain expanations.
    I will post my code after class, be on the lookout within the next hour or so, thanks for the help!
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    616
    Rep Power
    65
    When you work with mutable objects like lists you have to be careful about doing an alias copy:
    Code:
    # testing aliased (same address) objects
    q = [0, 0]
    
    mylist = [q, q]
    
    # mylist[0] and mylist[1] have the same address
    print(id(mylist[0]), id(mylist[1]))
    
    '''possible result (same id address) >>>
    (40117872, 40117872)
    '''
    
    # q[:] makes a true copy of list q and avoids alias copy
    # [:] is a slicing operator
    mylist = [q, q[:]]
    
    # test it ...
    print(id(mylist[0]), id(mylist[1]))
    
    '''possible result (different id address) >>>
    (40117872, 40119112)
    '''
    Last edited by Dietrich; November 4th, 2012 at 09:04 AM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo