What's the idiomatic syntax for prepending to a short python list?
The s.insert(0, x)
form is the most common.
Whenever you see it though, it may be time to consider using a collections.deque instead of a list.
If you can go the functional way, the following is pretty clear
new_list = [x] + your_list
Of course you haven't inserted x
into your_list
, rather you have created a new list with x
preprended to it.
What's the idiomatic syntax for prepending to a short python list?
You don't usually want to repetitively prepend to a list in Python.
If it's short, and you're not doing it a lot... then ok.
list.insert
The list.insert
can be used this way.
list.insert(0, x)
But this is inefficient, because in Python, a list
is an array of pointers, and Python must now take every pointer in the list and move it down by one to insert the pointer to your object in the first slot, so this is really only efficient for rather short lists, as you ask.
Here's a snippet from the CPython source where this is implemented - and as you can see, we start at the end of the array and move everything down by one for every insertion:
for (i = n; --i >= where; )
items[i+1] = items[i];
If you want a container/list that's efficient at prepending elements, you want a linked list. Python has a doubly linked list, which can insert at the beginning and end quickly - it's called a deque
.
deque.appendleft
A collections.deque
has many of the methods of a list. list.sort
is an exception, making deque
definitively not entirely Liskov substitutable for list
.
>>> set(dir(list)) - set(dir(deque))
{'sort'}
The deque
also has an appendleft
method (as well as popleft
). The deque
is a double-ended queue and a doubly-linked list - no matter the length, it always takes the same amount of time to preprend something. In big O notation, O(1) versus the O(n) time for lists. Here's the usage:
>>> import collections
>>> d = collections.deque('1234')
>>> d
deque(['1', '2', '3', '4'])
>>> d.appendleft('0')
>>> d
deque(['0', '1', '2', '3', '4'])
deque.extendleft
Also relevant is the deque's extendleft
method, which iteratively prepends:
>>> from collections import deque
>>> d2 = deque('def')
>>> d2.extendleft('cba')
>>> d2
deque(['a', 'b', 'c', 'd', 'e', 'f'])
Note that each element will be prepended one at a time, thus effectively reversing their order.
Performance of list
versus deque
First we setup with some iterative prepending:
import timeit
from collections import deque
def list_insert_0():
l = []
for i in range(20):
l.insert(0, i)
def list_slice_insert():
l = []
for i in range(20):
l[:0] = [i] # semantically same as list.insert(0, i)
def list_add():
l = []
for i in range(20):
l = [i] + l # caveat: new list each time
def deque_appendleft():
d = deque()
for i in range(20):
d.appendleft(i) # semantically same as list.insert(0, i)
def deque_extendleft():
d = deque()
d.extendleft(range(20)) # semantically same as deque_appendleft above
and performance:
>>> min(timeit.repeat(list_insert_0))
2.8267281929729506
>>> min(timeit.repeat(list_slice_insert))
2.5210217320127413
>>> min(timeit.repeat(list_add))
2.0641671380144544
>>> min(timeit.repeat(deque_appendleft))
1.5863927800091915
>>> min(timeit.repeat(deque_extendleft))
0.5352169770048931
The deque is much faster. As the lists get longer, I would expect a deque to perform even better. If you can use deque's extendleft
you'll probably get the best performance that way.