what does 'a=b=c=[]' do

Eric einazaki668 at yahoo.com
Wed Dec 21 17:25:17 EST 2011


Is it true that if I want to create an array or arbitrary size such
as:
   for a in range(n):
      x.append(<some function...>)

I must do this instead?
   x=[]
   for a in range(n):
      x.append(<some function...>)

Now to my actual question.  I need to do the above for multiple arrays
(all the same, arbitrary size).  So I do this:
   x=y=z=[]
   for a in range(n):
      x.append(<some function...>)
      y.append(<some other function...>)
      z.append(<yet another function...>)

Except it seems that I didn't create three different arrays, I created
one array that goes by three different names (i.e. x[], y[] and z[]
all reference the same pile of numbers, no idea which pile).

This surprises me, can someone tell me why it shouldn't?  I figure if
I want to create and initialize three scalars the just do "a=b=c=7",
for example, so why not extend it to arrays.  Also, is there a more
pythonic way to do "x=[], y=[], z=[]"?

It's a slick language but I still have trouble wrapping my brain
around some of the concepts.

TIA,
eric



More information about the Python-list mailing list