4 ms·
If you are copying many lists, or copying many large lists you still want to use the slice method ( new_list = old_list[:] ) as it is faster then list(). It is
by mgrouchy 15y ago
If you are copying many lists, or copying many large lists you still want to use the slice method ( new_list = old_list[:] ) as it is faster then list(). It is also the fastest method of copying lists if you consider copy.copy() and copy.deepcopy() as well.
The only caveat here is if you are copying a list of lists, you have to use copy.deepcopy() if you want the lists inside your lists to actually be copied.
- mrmekon 15y agoTested copy(), [:], and list(): >>> t1 = timeit.Timer('copy.copy(orig)','import copy;import random;orig = [random.randint(0,255) for r in xrange(100000)];') >>> t2 = timeit.Timer('orig[:]','import copy;import random;orig = [random.randint(0,255) for r in xrange(100000)];') >>> t3 = timeit.Timer('list(orig)','import copy;import random;orig = [random.randint(0,255) for r in xrange(100000)];') >>> print t1.timeit(10000)/10000 0.00036607401371 >>> print t2.timeit(10000)/10000 0.000416543197632 >>> print t3.timeit(10000)/10000 0.000372415995598 Probably not the world's most ideal testing here. I have no idea if/how Python caches, for instance. But slice notation continually comes out the slowest in this crummy example.
- mgrouchy 15y agoNot at my computer right now to whip up a simple benchmark(phone), but there is a pretty exhaustive benchmark here: http://stackoverflow.com/questions/2612802/how-to-clone-a-list-in-python http://stackoverflow.com/questions/2612802/how-to-clone-a-li...
- mrmekon 15y agoWith smaller lists, the results in my test change: >>> t1 = timeit.Timer('copy.copy(orig)','import copy;import random;orig = [random.randint(0,255) for r in xrange(10)];') >>> t2 = timeit.Timer('orig[:]','import copy;import random;orig = [random.randint(0,255) for r in xrange(10)];') >>> t3 = timeit.Timer('list(orig)','import copy;import random;orig = [random.randint(0,255) for r in xrange(10)];') >>> print t1.timeit(10000) 0.0183310508728 >>> print t2.timeit(10000) 0.00397896766663 >>> print t3.timeit(10000) 0.00760293006897 Which is similar to those results. This implies more setup cost for copy() and list(), but after that they're faster.
- hagy 15y agoIf not for noise, list() should always outperform copy() as copy() just calls list() internally (specifically type(l)(l)), and also incurs the cost of several interrupted function calls. Also, the minor difference in slice vs. list() for large lists are likely platform dependent and highly sensitive to the details of branch prediction and cache.
- RK 15y agoThe only caveat here is if you are copying a list of lists, you have to use copy.deepcopy() if you want the lists inside your lists to actually be copied. This caught me bad once. I wish it were on the page linked in the post (and others like it).