Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

np.add.at() in NumPy: Add Values at Repeated Indices

NumPy’s np.add.at() applies in-place additions for every index occurrence, including duplicates. See how it differs from buffered advanced-index assignment.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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 repeated index is not deduplicated; both updates are applied to the same array element.

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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Rank #2
NumPy - Python Library for Software Developers, Programmers T-Shirt
  • 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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.