Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use np.add.at(a, indices, values) when every occurrence in an index list must contribute to an in-place addition, including duplicates. Unlike a[indices] += values, which can buffer advanced-indexed values and update a repeated position only once, np.add.at() applies each update separately.

What np.add.at() does

np.add.at() is the indexed, unbuffered in-place form of NumPy’s addition ufunc. It updates the original array a at the specified indices rather than returning a separate array. The NumPy v2.1 API reference describes it as an unbuffered in-place operation on the operand for elements specified by the indices.

Because updates are applied for each occurrence, repeated indices count repeatedly. In this example, index 2 appears twice, so that element increases by two:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import numpy as np

a = np.array([1, 2, 3, 4])
np.add.at(a, [0, 1, 2, 2], 1)
print(a)  # [2, 3, 5, 4]

The original array is modified in place: its values after the call are [2, 3, 5, 4].

Why a[indices] += values can give a different result

With advanced indexing, NumPy may buffer the selected values before performing an augmented assignment. If an index occurs more than once, that buffered operation does not necessarily apply the increment once per occurrence. The documented example makes the distinction clear:

Operation Result for repeated index 0
a[[0, 0]] += 1 In NumPy’s documented example, the first element is incremented once because of buffering.
np.add.at(a, [0, 0], 1) The first element is incremented twice, once for each occurrence.

These results assume equivalent initial arrays. The difference is about duplicate-index semantics, not a general claim that one expression is faster. NumPy’s ufunc basics guide also explains the no-buffering behavior in the context of advanced indexing.

Choose the operation based on duplicate indices

  • Use np.add.at() when every repeated index must apply its own addition.
  • Use ordinary advanced-index augmented assignment only when its buffering behavior is acceptable. If the indices are unique, the repeated-index distinction described here does not arise.

The NumPy documentation does not establish a universal performance recommendation for these alternatives. If speed matters for a particular workload, benchmark that workload rather than assuming one form is faster.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Signature, index forms, and broadcasting

The method signature is ufunc.at(a, indices, b=None, /). For addition, call it as np.add.at(a, indices, b). For a multidimensional array, indices can be a tuple of array-like index objects or slices; the values in b must be broadcastable over the indexed or sliced operand. See the NumPy v2.1 API reference for the method’s index and broadcasting details.

at is a method on NumPy universal functions, or ufuncs, which operate element by element; addition is one such ufunc. The current stable ufunc reference lists at as an unbuffered in-place method.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.