Python "IN PLACE" functions

Paolo picture Paolo · Mar 15, 2011 · Viewed 16.3k times · Source

What's the particular reason that some functions in Python operate "IN PLACE", like [].sort and [].reverse, while some others like [].append not?

Answer

Martin Tournoij picture Martin Tournoij · Mar 15, 2011

According to my Programming Python 4th edition:

By default, pop is equivalent to fetching, and then deleting, the last item at offset −1. With an argument, pop deletes and returns the item at that offset—list.pop(-1) is the same as list.pop(). For in-place change operations like append, insert, del, and pop, no new list is created in memory, so execution is quick (performance may further de- pend upon which end is the “top,” but this in turn depends on Python’s current list implementation, as well as measurement concepts we’ll explore later).

There is actually a whole section devoted to this, but this pretty much answers your question.