Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

list.insert

Inserts an item before the requested position in a mutable list.

Version: Python 3.14; repository image, production runtime not attested

Syntax

a.insert(index, value)

Inserts an item before the requested position in a mutable list.

A representable index outside either end is clipped to the closest insertion point.

Inserts before an available position, clipping far-out positions to the beginning or end. Thus -100 prepends one and 100 appends four. A direct read with these out-of-range indices would raise IndexError. Clipping applies only to representable indices; CPython 3.14 raises OverflowError for 10**100.

a = [1]
try:
    a.insert(10**100, 3)
except OverflowError:
    assert a == [1]
else:
    raise AssertionError

Examples

a = [2, 3]
a.insert(-100, 1)
a.insert(100, 4)
print(a)

Result

[1, 2, 3, 4]

Inserts before an available position, clipping far-out positions to the beginning or end. Thus -100 prepends one and 100 appends four. A direct read with these out-of-range indices would raise IndexError. Clipping applies only to representable indices; CPython 3.14 raises OverflowError for 10**100.

Sources