Thursday 22 November 2012

Python: list A = A + [x,y] VS A.append

So this is my first post on this blog.. and my first ever in life.. :)

It was difficult to choose from where to start but finally.. I decided upon a problem I recently met with.

A = A + [x]  VS  A.append(x)


So I was coding for randomized algorithm to find kth smallest element in a given list of random numbers when I got struck with a mind boggling problem, my friend who used perfectly same algorithm could find answer for lists as long as a million elements in just matter of seconds while for mine... after 10,000 my laptop was like.. "goodbye.. hopefully our paths will cross again".So I looked into it and found that difference was in the command to manipulate list. I wondered that when lists are python are not immutable, why would there be a difference So that's what I learned ..

Whenever we do a A = A + [1,2,3]  for any programming language it is equivalent to B = A+ [1,2,3] because the right side of '=' is evaluated before its value is assigned to variable at left side.
Thus, python doesn't sees that 'we are just adding to a list', instead it assumes that we are just creating a new list.
Lets analyze command B = A + [1,2] in python (for now forget A = A+ A[1,2])
  • A, B, [1,2,] are three separate lists
  • B should be assigned a list that is concatenation of A and [1,2]
  • value of A should not change at the end of whole operation
The third point leads python to the decision of creating a temporary list where it copies the content of both A and [1,2] . This causes copying of both the lists into a new list and then assigning it to A again (for A = A + [1,2]).
Instead if i would have used A.append(), python would have understood that it doesn't need to copy the whole list, just add some data to its end. Now you can very well imagine how painful it would have been for python to copy lists of the magnitude of million elements again and again and again.

So, next time you need to add something to a list in python, remember : A.append was provided to you for a reason by python development team. !!

Hope I helped you or nonetheless, added a bit to your terabytes of knowledge :)
Comments and Suggestions are most welcomed.

We can't solve problems by using the same kind of thinking we used when we created them       
 -Albert Einstein