What do I mean if you rewrite a list with a list of the same form, will it keep the first list? For instance:
point = [1, 2]
for x in xrange(10000000):
point = [x, x + 1]
Does python use a list of from point = [1, 2]in each iteration of the loop, just updating the links in point[0]and point[1]to xand x + 1? Or does he create a new list at each iteration and throw away the old one? In other words, this is the equivalent of performance
point = [1, 2]
for x in xrange(10000000):
point[0] = x
point[1] = x + 1
I'm just wondering if python does this type of optimization under the hood
Edit: In addition to what everyone said below, I compared it out of my own curiosity.
On my venerable thinkpad Core2Duo:
point = [1, 2]
for x in xrange(10000000):
point = [x, x + 1]
# Result:
# real 0m6.164s
point = [1, 2]
for x in xrange(10000000):
point[0] = x
point[1] = x + 1
# Result:
# real 0m3.623s
source
share