Use np.add.at(a, indices, values) when every occurrence in an index list must count. Unlike buffered advanced-index assignment such as a[indices] += values, np.add.at() processes repeated indices one by one, so duplicate positions receive multiple additions.
What np.add.at() does
np.add.at(a, indices, b) performs an unbuffered, in-place addition on a at the positions selected by indices. It is the at method of NumPy’s addition ufunc; ufuncs apply operations element by element. See the NumPy v2.1 API reference and the NumPy ufunc reference.
As an Amazon Associate I earn from qualifying purchases.
Why repeated indices matter
With duplicate indices, np.add.at() applies the addition for each occurrence. NumPy’s documented example starts with an array, adds 1 at indices [0, 1, 2, 2], and ends with position 2 incremented twice:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
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 repeated index is not deduplicated; both updates are applied to the same array element.
#1 Best Overall
Why a[indices] += values can produce a different result
NumPy’s advanced-index operation can buffer the selected values before writing them back. As a result, repeated indices in a[indices] += values do not necessarily accumulate each update. In NumPy’s documented example, a[[0, 0]] += 1 increments the first element only once, while np.add.at(a, [0, 0], 1) increments it twice. The difference is buffering, not the choice of addition operator. The API reference describes this behavior, and NumPy’s ufunc basics guide explains the no-buffering behavior in the context of advanced indexing.
| Approach | What happens with duplicate indices | Choose it when |
|---|---|---|
np.add.at(a, indices, b) |
Each occurrence is applied, including repeats. | Every indexed update must count. |
a[indices] += b |
Advanced-index buffering can prevent repeated updates from accumulating; NumPy’s example increments the duplicated position once. | The buffered advanced-index behavior is acceptable for the task. |
How to choose
- Use
np.add.at()when indices can repeat and each occurrence represents a separate addition. - If indices are unique, the repeated-index difference described here does not arise.
- Do not assume one approach is universally faster: the cited NumPy documentation establishes the behavior, not a general performance recommendation. Measure with the actual data and workload if speed matters.
Signature and multidimensional indexing
The documented signature is ufunc.at(a, indices, b=None, /). For addition, the corresponding call is np.add.at(a, indices, b). In multidimensional cases, indices can be a tuple of array-like index objects or slices, and b must be broadcastable over the indexed or sliced operand. Consult the NumPy v2.1 API reference for the full indexing details. The at method was introduced in NumPy 1.8.0.
Quick Recap
Best Value
Rank #4
Rank #2
- NumPy is perfect for data scientists and engineers using Python. NumPy powers machine learning, financial modeling, and AI development. NumPy is essential for data analysis, physics research, big data processing in tech, and science research analytics
- NumPy offers mathematical functions, random number generators, linear algebra routines, Fourier transforms. NumPy Python library adds support for large multi-dimensional arrays and matrices, with high-level mathematical functions to operate on these arrays
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
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.




