Python 3 reference · List Methods

Python List insert() Method

Insert an item at a given position, shifting later items to the right. Index 0 adds to the front.

Python 3 Since 3.0

Syntax

list.insert(index, item)

Parameters

ParameterDescription
index Position to insert before; past the end appends, negative counts from the end.
item The value.

Return value

None.

Examples

Insert at the front and endTry it yourself
queue = ["b", "c"]
queue.insert(0, "a")
queue.insert(len(queue), "d")
print(queue)

Output

['a', 'b', 'c', 'd']
Many inserts at the front? Use dequeTry it yourself
from collections import deque
q = deque(["b", "c"])
q.appendleft("a")   # fast for adding to the front
print(list(q))

Output

['a', 'b', 'c']

Inserting near the front of a long list is slow (every later item moves). For queues, use collections.deque.

Version history

VersionChange
Python 3.0Added