Skip to main content

How to Use Inplace Operators in Python

Python's operator module provides functions that perform inplace operations - combining a computation and an assignment into a single function call. These functions mirror Python's augmented assignment operators (+=, -=, *=, etc.) and are especially useful when you need to pass an operator as an argument to higher-order functions like map(), reduce(), or custom callbacks.

In this guide, you will learn what inplace operators are, how each function works with practical examples, and when to use the operator module functions instead of standard operators.

What Are Inplace Operators?

An inplace operator performs an operation and assigns the result back in one step. For example, x += 5 adds 5 to x and stores the result in x. The operator module provides equivalent functions:

import operator

# These two lines are equivalent:
x += y
x = operator.iadd(x, y)

The key difference is that operator.iadd() is a callable function - you can store it in a variable, pass it to other functions, or use it in dispatch tables.

Mutable vs. immutable behavior

For mutable types like lists, inplace operators modify the original object directly. For immutable types like integers, strings, and tuples, a new object is created and returned - the original value cannot be changed.

import operator

# Mutable: list is modified in place
my_list = [1, 2, 3]
operator.iadd(my_list, [4, 5])
print(my_list) # [1, 2, 3, 4, 5] (same object, modified)

# Immutable: integer creates a new object
x = 10
y = operator.iadd(x, 5)
print(x) # 10 (original unchanged)
print(y) # 15 (new value returned)

Output:

[1, 2, 3, 4, 5]
10
15

iadd() - Inplace Addition (+=)

Adds two values and returns the result. Equivalent to a += b:

import operator

result = operator.iadd(2, 3)

print(f"2 + 3 = {result}")

Output:

2 + 3 = 5

With mutable types like lists, iadd() extends the list in place:

import operator

numbers = [1, 2, 3]
operator.iadd(numbers, [4, 5])

print(numbers)

Output:

[1, 2, 3, 4, 5]

isub() - Inplace Subtraction (-=)

Subtracts the second value from the first and returns the result. Equivalent to a -= b:

import operator

result = operator.isub(2, 3)

print(f"2 - 3 = {result}")

Output:

2 - 3 = -1

imul() - Inplace Multiplication (*=)

Multiplies two values and returns the result. Equivalent to a *= b:

import operator

result = operator.imul(2, 3)

print(f"2 * 3 = {result}")

Output:

2 * 3 = 6

With sequences, imul() repeats the sequence:

import operator

word = "ab"
result = operator.imul(word, 3)

print(result)

Output:

ababab

itruediv() - Inplace True Division (/=)

Divides the first value by the second and returns a float. Equivalent to a /= b:

import operator

result = operator.itruediv(10, 5)

print(f"10 / 5 = {result}")

Output:

10 / 5 = 2.0

Note that the result is always a float, even when the division is exact.

imod() - Inplace Modulus (%=)

Returns the remainder of dividing the first value by the second. Equivalent to a %= b:

import operator

result = operator.imod(10, 6)

print(f"10 % 6 = {result}")

Output:

10 % 6 = 4

iconcat() - Inplace Concatenation (+= for Sequences)

Concatenates two sequences and returns the result. This works with strings, lists, tuples, and other sequence types:

import operator

first = "tutorial"
second = "reference"

result = operator.iconcat(first, second)

print(f"Concatenated string: {result}")

Output:

Concatenated string: tutorialreference

With lists, iconcat() modifies the first list in place (since lists are mutable):

import operator

list_a = [1, 2, 3]
list_b = [4, 5, 6]

operator.iconcat(list_a, list_b)

print(list_a)

Output:

[1, 2, 3, 4, 5, 6]
iconcat() only works with sequences

Attempting to use iconcat() with non-sequence types raises a TypeError:

import operator

# ❌ Integers are not sequences
operator.iconcat(5, 10)
# TypeError: 'int' object can't be concatenated

For numeric addition, use iadd() instead.

Complete Reference Table

FunctionEquivalent OperatorOperationExampleResult
operator.iadd(a, b)a += bAdditioniadd(2, 3)5
operator.isub(a, b)a -= bSubtractionisub(2, 3)-1
operator.imul(a, b)a *= bMultiplicationimul(2, 3)6
operator.itruediv(a, b)a /= bTrue divisionitruediv(10, 5)2.0
operator.imod(a, b)a %= bModulusimod(10, 6)4
operator.iconcat(a, b)a += b (sequences)Concatenationiconcat("ab", "cd")"abcd"

Practical Example: Using Inplace Operators as Callbacks

The primary advantage of the operator module functions is that they are first-class callable objects. This makes them useful in functional programming patterns:

import operator
from functools import reduce

# Sum all numbers in a list using iadd as the reducer
numbers = [10, 20, 30, 40, 50]
total = reduce(operator.iadd, numbers)

print(f"Sum of {numbers} = {total}")

Output:

Sum of [10, 20, 30, 40, 50] = 150

You cannot pass the += operator directly to reduce(), but operator.iadd makes it possible.

Building a Dynamic Calculator

import operator

# Map operation names to inplace operator functions
operations = {
"add": operator.iadd,
"subtract": operator.isub,
"multiply": operator.imul,
"divide": operator.itruediv,
"modulus": operator.imod,
}

def calculate(op_name, a, b):
if op_name not in operations:
return f"Unknown operation: {op_name}"
return operations[op_name](a, b)

print(calculate("add", 15, 7))
print(calculate("multiply", 6, 8))
print(calculate("divide", 100, 3))

Output:

22
48
33.333333333333336

Common Mistake: Expecting Immutable Types to Change In Place

A frequent source of confusion is expecting inplace operators to modify immutable values:

import operator

# ❌ Expecting x to change: but integers are immutable
x = 10
operator.iadd(x, 5)
print(x) # Still 10!

Output:

10

The function returns the new value, but since integers are immutable, x is not modified. You must capture the return value:

import operator

# ✅ Correct: capture the returned value
x = 10
x = operator.iadd(x, 5)
print(x) # 15

Output:

15
Rule of thumb
  • Mutable types (lists, dicts, sets): The original object is modified by inplace operators.
  • Immutable types (int, float, str, tuple): A new object is returned. Always assign the result back to your variable.

Conclusion

Python's inplace operator functions in the operator module (iadd(), isub(), imul(), itruediv(), imod(), and iconcat()) provide callable equivalents of augmented assignment operators.

They are most valuable when you need to pass an operator as a function argument to higher-order functions like reduce(), sorting keys, or dynamic dispatch tables.

Remember that for immutable types, these functions return new values without modifying the originals, so always capture the return value. For mutable types like lists, the operations modify the object in place, making them truly "inplace."