Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

strange append

0 views
Skip to first unread message

E.Nurminski

unread,
Oct 2, 2006, 12:49:05 AM10/2/06
to pytho...@python.org

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 ?

James Stroud

unread,
Oct 2, 2006, 1:15:02 AM10/2/06
to

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/

Gary Herron

unread,
Oct 2, 2006, 1:29:22 AM10/2/06
to E.Nurminski, pytho...@python.org
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 ?
>
It's best, in Python, to consider *everything* to be a reference to an
object. Most actions will work with a reference to an existing object,
and creating a new reference to an object will almost never create a
copy of the object. If you *do* want to create an object, you may
consider using the copy module:

http://docs.python.org/lib/module-copy.html

(But I must point out, that in 12 years of programming Python, I've
hardly ever used that module.)

Gary Herron


Bruno Desthuilliers

unread,
Oct 2, 2006, 5:41:15 AM10/2/06
to
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 'on...@xiludom.gro'.split('@')])"

James Stroud

unread,
Oct 2, 2006, 3:53:57 PM10/2/06
to
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])

0 new messages