Operators

-= — Subtraction assignment

Reduce a numeric value or remove elements from a mutable set by difference. A set updates in place, while a frozenset is replaced by a new result as needed. Ordinary lists do not implement subtraction. The __isub__() hook may modify its receiver or supply a replacement. If absent or declining with NotImplemented, normal binary dispatch applies. The result is stored back into the target; this is a statement, not an expression. A failed store does not guarantee rollback.

Formal Syntax & Structure

python syntaxpython
target -= expression

Parameters & Attributes

NameTypeDescription
targetobjectExisting assignable name, attribute, item or slice; its current value participates in the operation.
rightnumber or compatible setValue used as the right operand of the operation.

Working Example

Example Snippetpython
balance = 40balance -= 12active = {1, 2, 3}alias = activeactive -= {2, 4}print(balance)print(sorted(alias))
Python Reference