I have run this list copying experiment and found one of the times to be much quicker than the others, as far as I can tell it does copy the list, unless someone can explain otherwise?
import time
def test(d):
s = time.time()
for i in range(20000000):
d['b'] = d['a'].copy()
e = time.time()
print (".copy(): "+str(e-s))
s = time.time()
for i in range(20000000):
d['b'][:] = d['a']
e = time.time()
print ("[:] on left: "+str(e-s))
s = time.time()
for i in range(20000000):
d['b'] = d['a'][:]
e = time.time()
print ("[:] on right: "+str(e-s))
d = {'a': [1,2,3,4],
'b': []}
test(d)
results:
.copy(): 4.150316476821899
[:] on left: 2.535377025604248
[:] on right: 4.140159606933594
To prove [:] on left is copying?
d['b'][:] = d['a']
del d['a'][1]
d['a'][0] = "testing"
print (d)
prints:
{'a': ['testing', 3, 4], 'b': [1, 2, 3, 4]}
Can anyone explain what's going on here?