I often use:
lst2 = lst1 * 1
If lst1 contains other containers (like other lists), you should use deepcopy from the lib copy, as shown by Mark.
UPDATE: Explanation of Depth
>>> a = range(5) >>> b = a*1 >>> a,b ([0, 1, 2, 3, 4], [0, 1, 2, 3, 4]) >>> a[2] = 55 >>> a,b ([0, 1, 55, 3, 4], [0, 1, 2, 3, 4])
As you can see only changed ... I will try now with a list of lists
>>> >>> a = [range(i,i+3) for i in range(3)] >>> a [[0, 1, 2], [1, 2, 3], [2, 3, 4]] >>> b = a*1 >>> a,b ([[0, 1, 2], [1, 2, 3], [2, 3, 4]], [[0, 1, 2], [1, 2, 3], [2, 3, 4]])
Not readable, let me print it with: for:
>>> for i in (a,b): print i [[0, 1, 2], [1, 2, 3], [2, 3, 4]] [[0, 1, 2], [1, 2, 3], [2, 3, 4]] >>> a[1].append('appended') >>> for i in (a,b): print i [[0, 1, 2], [1, 2, 3, 'appended'], [2, 3, 4]] [[0, 1, 2], [1, 2, 3, 'appended'], [2, 3, 4]]
Do you see it? It is also added to b [1], so b [1] and [1] are the same object. Now try with deepcopy
>>> from copy import deepcopy >>> b = deepcopy(a) >>> a[0].append('again...') >>> for i in (a,b): print i [[0, 1, 2, 'again...'], [1, 2, 3, 'appended'], [2, 3, 4]] [[0, 1, 2], [1, 2, 3, 'appended'], [2, 3, 4]]