By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

strange append

P: n/a

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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.