The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Appending two lists together?
Discuss Appending two lists together? in the Python Programming forum on Dev Shed. Appending two lists together? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 31st, 2012, 12:57 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 15
Time spent in forums: 4 h 13 m 33 sec
Reputation 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??
|

October 31st, 2012, 01:46 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
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.
|

October 31st, 2012, 03:12 PM
|
 |
Contributing User
|
|
|
|
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!
|

October 31st, 2012, 03:28 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 15
Time spent in forums: 4 h 13 m 33 sec
Reputation Power: 0
|
|
Quote: | 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!
|

November 4th, 2012, 08:15 AM
|
 |
Contributing User
|
|
|
|
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)
'''
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
Last edited by Dietrich : November 4th, 2012 at 09:04 AM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|