473,508 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strange append


Hello to all good people

I am new to the great Py so am quite puzzled by the following code

---------------

res = []
x = [ 1, 1 ]
for i in xrange(0,5):
res.append(x)
x[1] = x[1] + 1
print "x = ", x
print "res = ", res

---------------

Looks like it puts smth like reference to 'x' into 'res' list, instead of
value. But if I want a value should I use a different method or what ?

Evgeni

P.S. Could not easily find the issue in the manual/tutorial can you point
me out to smth relevant ?

Oct 2 '06 #1
3 1225
E.Nurminski wrote:
Hello to all good people

I am new to the great Py so am quite puzzled by the following code

---------------

res = []
x = [ 1, 1 ]
for i in xrange(0,5):
res.append(x)
x[1] = x[1] + 1
print "x = ", x
print "res = ", res

---------------

Looks like it puts smth like reference to 'x' into 'res' list, instead of
value. But if I want a value should I use a different method or what ?

Evgeni

P.S. Could not easily find the issue in the manual/tutorial can you point
me out to smth relevant ?
Yes the same reference gets added every time.

res = []
x = [1, 1]
for i in xrange(0,5):
newx = x[:] # copy the x
res.append(newx) # append the copy
newx[1] += 1 # shorthand
print "newx = %s" % newx # basic formatting
print "res = %s" % res # should be what you expect

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Oct 2 '06 #2
E.Nurminski wrote:
Hello to all good people

I am new to the great Py so am quite puzzled by the following code

---------------

res = []
x = [ 1, 1 ]
for i in xrange(0,5):
res.append(x)
x[1] = x[1] + 1
print "x = ", x
print "res = ", res

---------------

Looks like it puts smth like reference to 'x' into 'res' list, instead of
value. But if I want a value should I use a different method or what ?
What difference do you make between "values" and "references" ?-)

Hint 1: in Python, all you have are objects. Yes, even strings and
numbers etc...
Evgeni

P.S. Could not easily find the issue in the manual/tutorial can you point
me out to smth relevant ?
Hint 2 : Python has something very nice which is the interactive python
shell. This lets you try code snippets and see by yourself how it really
works :

bruno@bousin ~ $ python
Python 2.4.3 (#1, Sep 29 2006, 20:26:46)
[GCC 4.1.1 (Gentoo 4.1.1-r1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>mylist = [1, "aaa"]
mylist.append(42)
mylist.append("lala")
mylist
[1, 'aaa', 42, 'lala']
>>>
HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 2 '06 #3
E.Nurminski wrote (off the list):
the intention was to have

newx = [1, 2]
res = [[1, 2]]
newx = [1, 3]
res = [[1, 2], [1, 3]]
newx = [1, 4]
res = [[1, 2], [1, 3], [1, 4]]
newx = [1, 5]
res = [[1, 2], [1, 3], [1, 4], [1, 5]]
newx = [1, 6]
res = [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6]]
This shows how valuable it is to post your expected results with your code.

The way most experienced python programmers might do this is with list
comprehension:

res = [[1, i] for i in xrange(1,7)]

For newer programmers, this might make more sense:

res = []
for i in xrange(1,7):
res.append([1,i])

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Oct 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2579
by: Funduk | last post by:
Hello, So I've been playing with Python and Pygame for a while and I decided I wanted to make a real executable so I could send that stuff over to my friends to show off my <sarcasm>maad...
24
1995
by: brian.bird | last post by:
Can anyone explain the behaviour of python when running this script? >>> def method(n, bits=): .... bits.append(n) .... print bits .... >>> method(1) >>> method(2)
5
3763
by: Amaryllis | last post by:
I'm trying to call a CL which is located on our AS400 from a Windows application. I've tried to code it in different ways, but I seem to get the same error every time. Does anyone have any clue...
1
948
by: Vb2Machines | last post by:
Hello to all I have an ASP.NET application that uses VB.NET as the background language and SQL server 2K as the back end database. I just recently converted this application from classic asp and I...
4
1159
by: Sean Hammond | last post by:
I've managed to create a scenario in which editing an object in a list of objects seems to edit every object in the list, rather than just the one. I'm totally stumped and wondered if anyone would...
20
1648
by: SpreadTooThin | last post by:
I have a list and I need to do a custom sort on it... for example: a = #Although not necessarily in order def cmp(i,j): #to be defined in this thread. a.sort(cmp) print a
5
1490
by: rconradharris | last post by:
A co-worker of mine came across some interesting behavior in the Python interpreter today and I'm hoping someone more knowledgeable in Python internals can explain this to me. First, we create...
8
1662
by: matthewperpick | last post by:
Check out this toy example that demonstrates some "strange" behaviour with keyword arguments and inheritance. ================================= class Parent: def __init__(self, ary = ):...
17
2253
by: Terry Olsen | last post by:
Given that variable dt = "3/31/2007", why does it produce the following exception on some machines? It works fine on my PC, but others have sent me this exception information because it threw up on...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.