Knowledge base / Python

numpy.put: replacing positions, wrapping and mutating an array

I trace the cells changed by put and distinguish wrapping from clipping. Fixed synthetic data and separate working arrays make every update visible.

Kajo RudzińskiPublished Updated 4 min read

Mechanical grippers replacing two blocks in the same grid.

To replace values at specified array positions, I can use numpy.put. The key condition is understanding that the supplied object will change. The entry on numpy.take concerns retrieval; this entry concerns writing. Small synthetic arrays make each update visible. The description follows NumPy 2.5 documentation.

Syntax and an in-place update

The official function signature is np.put(a, ind, v, mode='raise'): target array, positions, values and boundary mode. The method a.put(...) is the equivalent array operation.

import numpy as np

a = np.array([10, 20, 30, 40, 50])
np.put(a, [1, 3], [99, 77])
# a: [10, 99, 30, 77, 50]

Position 1 changes to 99 and position 3 to 77. Other positions keep their values. I do not assign the call back to a: writing a = np.put(...) would replace that name with None, because the operation does not return a new array. This is a common trap when moving from a function that retrieves a result to one that modifies its input.

A flat position is not a row number

put has no axis argument. Positions follow the array’s flattened element order:

m = np.array([[10, 20, 30],
              [40, 50, 60]])
np.put(m, [1, 4], [99, 77])
# m: [[10, 99, 30], [40, 77, 60]]

Positions 0, 1 and 2 belong to the first row; 3, 4 and 5 to the second. Position 4 therefore identifies the middle cell of the second row. I am not inserting a fourth row. The shape remains (2, 3) while values change. Before updating a larger matrix, I describe what each position means, so a technically valid index cannot conceal an incorrect mapping between records and cells.

If the task is to change a different column in each row, examine put_along_axis. It matches index arrays to slices along a specified axis. It is not an automatic replacement: establish the intended shape and whether positions are flat or separate for each row before choosing the operation.

A shorter values list repeats

Here I supply four positions and two replacement values:

b = np.array([10, 20, 30, 40, 50])
np.put(b, [0, 1, 3, 4], [8, 9])
# b: [8, 9, 30, 8, 9]

The assignments use 8, 9, 8 and 9. The middle value, 30, is not targeted. Repetition is useful when intentional, but could also hide missing update data. If the task requires one supplied replacement for each position, I would check those lengths instead of relying on the shorter list cycling through the destination positions.

I also consider the target data type. These examples use integer arrays and integer replacements. An assignment is not a request to redesign the entire type model automatically. If you need another kind of value, establish the appropriate dtype and conversion rules before changing the data. A successful operation does not establish that the replacement has retained its intended meaning.

wrap and clip reach different destinations

Each variant begins with a separate five-element array:

wrapped = np.array([10, 20, 30, 40, 50])
np.put(wrapped, [1, 7], [99, 77], mode='wrap')
# wrapped: [10, 99, 77, 40, 50]

clipped = np.array([10, 20, 30, 40, 50])
np.put(clipped, [1, 7], [99, 77], mode='clip')
# clipped: [10, 99, 30, 40, 77]

With five elements, index 7 wraps to 2, replacing 30 with 77. Clipping sends index 7 to the final position, 4, replacing 50. The default raise mode would reject 7. In clip mode, a negative index reaches the beginning rather than counting backwards from the end.

The earlier example wrapped index 10 in a five-element array but showed the last cell changing. That was incorrect: 10 wraps to 0. If 0 is also targeted, writes overlap. In introductory examples, I separate such operations to make the sequence explicit:

c = np.array([10, 20, 30, 40, 50])
np.put(c, [0, 2], [100, 300])
np.put(c, [10], [500], mode='wrap')
# c: [500, 20, 300, 40, 50]

The second call replaces the earlier 100 at position 0. It does not add 500 to 100. If the task requires accumulating updates or resolving conflicts, define that operation rather than allowing accidental overlapping assignments to decide it. Correcting an index alone cannot establish which update should win in your own process.

Two array racks, one preserved and one updated, with a looping rail behind the working rack.

An error may leave partial changes

The put reference warns that an exception in mode='raise' does not guarantee an unchanged target. I do not treat the function as a transaction. When I need to preserve the input, I first use an independent working copy. Writing working = source does not create that copy: both names refer to the same object.

The copies and views guide also explains the risk of modifying a slice that shares data. For a simple numeric array, I deliberately use source.copy() when updates should affect separate working data. I would not extend that statement into a guarantee of deeply copying arbitrary nested objects in an object array.

Exercise and choice of operation

Write [10, 20, 30, 40, 50] on paper. Target [0, 6] with values [8, 9]. Wrapping sends the second write to position 1, producing [8, 9, 30, 40, 50]. Clipping sends it to position 4, producing [8, 20, 30, 40, 9]. You can follow the rule without random positions or hidden state left over from an earlier example.

For a straightforward update to known cells, direct indexing may be clearer. I choose put when flat positions and its boundary mode fit the task. This entry offers no unmeasured speed guarantees. First establish what will change, whether the source must be preserved and how to inspect the effect of every write.

Key takeaways

Does numpy.put return a new array?

No. It changes the supplied array in place and returns None. Create a separate copy first if the source must be preserved.

Does put have an axis parameter?

No. Indices address the flattened element order. For separate positions along an axis, consider put_along_axis.

Does IndexError undo earlier assignments?

That is not guaranteed. The documentation warns that raise mode can leave the array partially modified before an exception.

AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.