4

What I mean is, if you over-write a list with a list of identical shape, does it keep the first list? For example:

point = [1, 2]
for x in xrange(10000000):
    point = [x, x + 1]

does python reuse the list from point = [1, 2] in each iteration of the loop, simply updating the references at point[0] and point[1] to x and x + 1? Or does it create a new list in each iteration and throw out the old one? Put another way, is that equivalent in performance to

point = [1, 2]
for x in xrange(10000000):
    point[0] = x
    point[1] = x + 1

I'm just curious if python does that type of optimization under the hood

Edit: In addition to what everyone said below, I benchmarked it for my own curiosity.

On my venerable thinkpad's 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
Joe Pinsonault
  • 537
  • 7
  • 16

5 Answers5

7

No, CPython does not re-use list objects. The line point = [x, x + 1] creates an entirely new object, and the previous object is destroyed if point was the only reference.

Python does reuse tuple objects (up to a limit), because Python internals create and destroy a lot of tuples in the normal course of a Python program. See How is tuple implemented in CPython?

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
4

Add this print to discover your answer:

point = [1, 2]
for x in xrange(10000000):
    point = [x, x + 1]
    print(id(point))

This will tell you the memory location of point, and hence whether it is reusing the instance or letting them be garbage collected.

wim
  • 338,267
  • 99
  • 616
  • 750
  • 1
    46614664 46637576 46614664 46637576 46614664 46637576 46614664 46637576 46614664 46637576 Interesting... – Joe Pinsonault Mar 04 '14 at 20:03
  • 1
    @wim: this is not that great an example, because Python will reuse *memory positions*. You'll see the same value re-appear. – Martijn Pieters Mar 04 '14 at 20:04
  • 1
    I know, but my intention was to give the OP a tool which they can use to investigate issues like this themselves. It is also interesting implementation detail to see how the memory positions are reused every second time like that. – wim Mar 04 '14 at 20:05
3

No. When you do point = [1, 2], what is actually happening is that Python creates a list [1, 2] and let point to be a reference to the memory address of this list. When you do point = [x, x+1], Python creates another brand new list and simply changes the memory address of what point references to to the new list. The original list [1, 2] is actually STILL IN memory but is not accessible by you and will be collected by Python automatic garbage collector later. You can do this:

point = [1, 2]
b = point
point = [3, 4]
print(b == point)

point2= [1, 2]
c = point2
point2[0] = 3
print(c == point2)

The output of this illustrates everything.

Xufeng
  • 6,452
  • 8
  • 24
  • 30
2

No, python doesn't reuse the lists when writing: point = [x,x+1], by the way, you can make in place assignment and, in this case, the list will remain the same.

For example:

point = [1,2]

for x in range(5): # Reusing point list
    point[:] = [x,x+1]

for x in range(5): # Creating new list
    point = [x,x+1]

as @wim stated, you can check the memory location with id(point)

Ruben Bermudez
  • 2,293
  • 14
  • 22
2

This line point = [x, x + 1] does 2 things, mainly:

  1. [x, x + 1] creates a new list. At this point, python does not know if you are going to use the list point in the creation of the list, or what you are going to do with it
  2. point = ... affects the newly created list to the previous list, and releasing the previous list.
njzk2
  • 38,969
  • 7
  • 69
  • 107