What is the best way to copy a list?

What is the best way to copy a list? I know the following methods, which one is better? Or is there another way?

lst = ['one', 2, 3] lst1 = list(lst) lst2 = lst[:] import copy lst3 = copy.copy(lst) 
+48
python
Oct 08 '08 at 20:11
source share
7 answers

If you want a shallow copy (elements are not copied), use:

 lst2=lst1[:] 

If you want to make a deep copy, use the copy module:

 import copy lst2=copy.deepcopy(lst1) 
+87
Oct 08 '08 at 20:14
source share
— -

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]] 
+18
Oct 08 '08 at 20:18
source share

You can also do:

 a = [1, 2, 3] b = list(a) 
+12
Oct 08 '08 at 20:31
source share

I like:

 lst2 = list(lst1) 

The advantage over lst1 [:] is that the same function applies to dicts:

 dct2 = dict(dct1) 
+6
Oct 08 '08 at
source share

Short lists, [:] are the best:

 In [1]: l = range(10) In [2]: %timeit list(l) 1000000 loops, best of 3: 477 ns per loop In [3]: %timeit l[:] 1000000 loops, best of 3: 236 ns per loop In [6]: %timeit copy(l) 1000000 loops, best of 3: 1.43 us per loop 

For larger lists, they are all the same:

 In [7]: l = range(50000) In [8]: %timeit list(l) 1000 loops, best of 3: 261 us per loop In [9]: %timeit l[:] 1000 loops, best of 3: 261 us per loop In [10]: %timeit copy(l) 1000 loops, best of 3: 248 us per loop 

For very large lists (I tried 50MM), they are pretty much the same anyway.

+3
Feb 11 '13 at 21:45
source share

You can also do this:

 import copy list2 = copy.copy(list1) 

This should do the same as Mark Roddy, a shallow copy.

+2
Oct 08 '08 at 20:23
source share

In terms of performance, there is the overhead of calling list() compared to slicing. Therefore, for short lists, lst2 = lst1[:] about two times faster than lst2 = list(lst1) .

In most cases, this probably outweighs the fact that list() more readable, but in narrow loops this can be a valuable optimization.

0
Jan 18 2018-12-18T00:
source share



All Articles