Python Glossary Русский

all Python 3operatorfunction

operator.setitem

sequences

Assigns by key or index: a[b] = c (mutates the object).

Syntax

operator.setitem(a, b, c)

It returns None and changes the object in place — there is nothing to assign the result to. It helps where an assignment statement is not allowed: inside a lambda, an event callback, or map used for its side effect. On a tuple or string it raises TypeError: immutable sequences do not support item assignment.

Examples

import operator
l = [0, 0]
operator.setitem(l, 0, 9)
print(l)   # → [9, 0]

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy