multivariable assignment

J dreadpiratejeff at gmail.com
Thu Dec 31 11:27:51 EST 2009


On Thu, Dec 31, 2009 at 11:13, davidj411 <davidj411 at gmail.com> wrote:
> I am not sure why this behavior is this way.
> at beginning of script, i want to create a bunch of empty lists and
> use each one for its own purpose.
> however, updating one list seems to update the others.
>
>>>> a = b = c = []
>>>> a.append('1')
>>>> a.append('1')
>>>> a.append('1')
>>>> c
> ['1', '1', '1']
>>>> a
> ['1', '1', '1']
>>>> b
> ['1', '1', '1']

That's because you're saying c is an empty list object, b is a pointer
to the same list object and a is a pointer to the same list object.
It looks better if you look at it like this:

a \
b  ------  []
c /

You just created one actual object and created three pointers to it.

Though I wondered something similar the other day...

obviously this won't work:

a,b,c = []

but does this do it properly:

a,b,c = [],[],[]

For now, in my newbieness, I've been just creating each one separately:

a=[]
b=[]
c=[]

Cheers,

Jeff
-- 

Jonathan Swift  - "May you live every day of your life." -
http://www.brainyquote.com/quotes/authors/j/jonathan_swift.html



More information about the Python-list mailing list