Why is it that when a variable is reassigned a new value in python new memory is allocated? Why can't modification take in place like it happens if an extra value is appended to a list, but when it is reassigned with the current list plus some new list, new memory is allotted.
>>> a=2
>>> id(a)
13332800
>>> a=a+2 # reassigning allocates new memory
>>> id(a)
13332752
>>> a=[1,2,3,4]
>>> id(a)
139923169899008
>>> a.append(2) # change takes in place
>>> id(a)
139923169899008
>>> a=a+[3,2] # this way causes new memory allocation
>>> id(a)
139923169899656
Is there any way to avoid reallocation of memory on every new assignment?