strange append

E

E.Nurminski

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 ?
 
J

James Stroud

E.Nurminski said:
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/
 
B

Bruno Desthuilliers

E.Nurminski said:
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

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
 
J

James Stroud

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top