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. Prepending to a deque runs in constant time. Prepending to a list runs in linear time.
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.