[Tutor] Copying list contents

Guillermo Fernandez guillermo.fernandez@epfl.ch
Mon, 29 Jul 2002 14:18:15 +0930


hi!

Thanks for the 
mylist=[0] * size
trick. It's neat and quick.

I'm working with lists and I manipulate often. I have discovered that
Python treates the lists like objects, so this operation:
>>> list1=[1, 2, 3]
>>> list2=list1
will not create two listes with the same arguments, but will create two
references to the same object:
>>> list1[2]=4
>>> list2
[1, 2, 4]

In that kind of easy lists there is no problem, I define a function
like:
def copylist(a):
	b=[]
	for element in a:
		b.append(element)
	return b

but I'm working with lists of lists of lists...
Is there a way of copying lists cleanly, or will I have to create a
recursive list copy function?

Thanks,

Guille