a trick with lists ?

Ed Leafe ed at leafe.com
Thu Feb 7 16:33:48 EST 2008


On Feb 7, 2008, at 3:19 PM, James Turk wrote:

> if you do
> a = [1,2,3]
> b = []
> b = a
>
> then assign: b[1] = 9
> now a[1] == 9 as well
>
> with a[:] = b you are actually getting a copy of the list rather than
> an alias


	Of course, this only works if 'b' is already a list. A more common  
and more general usage for making list copies would be:

a = [1,2,3]
b = a[:]

	In this usage, 'a' and 'b' are separate lists, but 'b' doesn't need  
to be defined as a list first.

-- Ed Leafe






More information about the Python-list mailing list