The list of indices indicating which elements you want to replace should be a Python list (or similar type), not a tuple. Different items in the selection tuple indicate that they should be selected from different axis dimensions.

Therefore, a[(2, 4, 6)] is the same as a[2, 4, 6], which is interpreted as the value at index 2 in the first dimension, index 4 in the second dimension, and index 6 in the third dimension.

The following code works correctly:

indexes = [2, 4, 6]
a[indexes] = newvalues

See also the page on Indexing from the numpy documentation, specifically the second 'Note' block in the introduction as well as the first 'Warning' under Advanced Indexing:

In Python, x[(exp1, exp2, ..., expN)] is equivalent to x[exp1, exp2, ..., expN]; the latter is just syntactic sugar for the former.

The definition of advanced indexing means that x[(1,2,3),] is fundamentally different than x[(1,2,3)]. The latter is equivalent to x[1,2,3] which will trigger basic selection while the former will trigger advanced indexing. Be sure to understand why this occurs.

Answer from luuk on Stack Overflow
🌐
Python Guides
pythonguides.com › python-numpy-replace
Replace Values In NumPy Array By Index In Python
May 16, 2025 - Here, I’m setting a minimum display rating of 3.0 by replacing any value below that threshold. The np.where() function evaluates the condition for each element and returns either the replacement value or the original value. ... Sometimes, you need to replace different values with different replacements. Here’s how to do that using NumPy’s advanced indexing: import numpy as np # Sample array of house prices (in thousands of dollars) house_prices = np.array([350, 420, 285, 510, 370, 425, 390, 480, 330, 560]) # Indices of houses we want to update indices = [1, 4, 7] # New prices for these houses new_prices = [450, 400, 500] # Replace the values house_prices[indices] = new_prices print(house_prices) # Output: [350 450 285 510 400 425 390 500 330 560]
🌐
ProjectPro
projectpro.io › recipes › replace-items-that-satisfy-condition-with-another-value-numpy-array
How to Replace Values in a NumPy Array? -
May 16, 2024 - Replacing values in NumPy arrays by index involves specifying the indices of elements you want to replace and assigning new values to them.
Discussions

python - Getting and replace values from numpy array based on list of index - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 4 Pythonic way to replace specific values with other values of numpy array within particular indices · 0 How to replace certain elements of a NumPy array via an index array More on stackoverflow.com
🌐 stackoverflow.com
python - Replace values of a numpy index array with values of a list - Stack Overflow
What would we do, if we want to change the elements at indexes which are multiple of given n, simultaneously. Like simultaneously change a[2],a[4],a[6].... for n = 2., what should be done? 2015-10-07T18:57:12.643Z+00:00 ... Save this answer. ... Show activity on this post. Instead of replacing the values one by one, it is possible to remap the entire array like this: Copyimport numpy ... More on stackoverflow.com
🌐 stackoverflow.com
November 29, 2017
How can I replace values in a Python NumPy array with the index of those values found in another array? - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Domain expertise still wanted: the latest trends in AI-assisted knowledge for... ... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... 26 Replace values of a numpy index array ... More on stackoverflow.com
🌐 stackoverflow.com
python - Replace an element in a numpy array at specific index - Stack Overflow
Such an object array is similar to a list, containing pointers to elements elsewhere in memory. If A should remain numeric, then you need to assign compatible numeric values. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 4 Pythonic way to replace specific values with other values of numpy ... More on stackoverflow.com
🌐 stackoverflow.com
April 26, 2017
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.put.html
numpy.put — NumPy v2.5 Manual
The indexing works on the flattened target array. put is roughly equivalent to: ... Target array. ... Target indices, interpreted as integers. ... Values to place in a at target indices. If v is shorter than ind it will be repeated as necessary. mode{‘raise’, ‘wrap’, ‘clip’}, optional · Specifies how out-of-bounds indices will behave. ... ‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python numpy howtos" › "how to replace values in numpy"
How to Replace Values in NumPy | Delft Stack
February 2, 2024 - To replace values in a NumPy array, use numpy.clip() or combine numpy.minimum() and numpy.maximum() to clamp values between two limits. For a condition such as “replace every value above 5,” use boolean indexing and assignment.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › replace elements in numpy array
r/learnpython on Reddit: Replace elements in numpy array
September 9, 2021 -

I have a function that converts angular values (degrees) into cardinal directions (N, NNW, ect). Here is what I have so far:

import numpy as np

def to_cardinal(values: np.ndarray):

    CARD_DIR = [
        "N",
        "NNE",
        "NE",
        "ENE",
        "E",
        "ESE",
        "SE",
        "SSE",
        "S",
        "SSW",
        "SW",
        "WSW",
        "W",
        "WNW",
        "NW",
        "NNW",
        "N",
    ]

    # If values over 360, scale down
    angle = np.remainder(vals_array, 360.0)

    # Divide by 22.5 and convert to int
    # On another note, not sure if np.around is necessary?
    index_arr = np.around((angle / 22.5)).astype(np.int16, copy=False)

    return index_arr

I return index_arr here because I'm not sure how to continue. Here is a sample input/output:

input = np.array((10, 350, 190, 100, 270, 30))
output = to_cardinal(input)
print(output)

>>> (0, 16, 8, 4, 12, 1)

These values are the indices of the CARD_DIR list that the degree values corresponds to. In words, I need to construct an array (or replace from index_arr) where each element is now the string from CARD_DIR instead of the integer that represents the index. I can imagine doing this in a loop, but how could this be vectorized?

In psuedo-code:

new_array = np.empty(size = index_arr.size)

for i, value in enumerate(index_arr):
    new_array[i] = CARD_DIR[value]

Also, not sure if np.around is necessary when casting a float to an int? Does the int type conversion round correctly already?

Edit: Figured it out, numpy is magical. See comment below! Still interested in type-casting from floats to ints, though.

🌐
stataiml
stataiml.com › posts › 40_replace_item_list_python
Python: Replace Value at Specific Index in List and Array - stataiml
May 14, 2024 - You can replace a value at a specific index in a list in Python by using direct indexing. The basic syntax for replacing a value at a specific index using direct indexing: # replacing the value at index 3 (remember in python index start at 0) ...
🌐
CloudxLab
cloudxlab.com › assessment › displayslide › 2534 › numpy-arrays-indexing-and-array-slicing
Numpy - Arrays - Indexing and Array Slicing | Automated hands-on| CloudxLab
Please print this NumPy array my_orig_arr2 to see its values (please note that only values at indexes 2, 3, 4 will be replaced, not the 5th index value) ... Please remember that this is only possible in a NumPy array, if you try to do this with a normal Python array, you will get an error. ... (2) Extract a portion of this NumPy array apple from index 1 to 4 (by using the below syntax) and store it in a variable apple_slice
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.put.html
numpy.put — NumPy v2.1 Manual
The indexing works on the flattened target array. put is roughly equivalent to: ... Target array. ... Target indices, interpreted as integers. ... Values to place in a at target indices. If v is shorter than ind it will be repeated as necessary. mode{‘raise’, ‘wrap’, ‘clip’}, optional · Specifies how out-of-bounds indices will behave. ... ‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis.
🌐
Finxter
blog.finxter.com › 5-efficient-ways-to-replace-values-in-python-numpy-arrays
5 Efficient Ways to Replace Values in Python Numpy Arrays – Be on the Right Side of Change
An intuitive way to replace values in a Numpy array is through basic indexing, which involves specifying conditions for which indices to replace.
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How to replace element in NumPy array? - Python - Data Science Dojo Discussions
April 26, 2023 - vec = np.random.randint(0, 100, size=10) print('Array: ',vec) # Find the index of the maximum value max_index = np.where(vec == max(vec)) # Replace the maximum value by 0 max_index[vec] = 0 print('Final_Array: ',vec) TypeError: only integer scalar arrays can be converted to a scalar index · Can you correct the code or provide me with different methods that replace elements of the array? ... This code creates a NumPy array of size 10, with random float values between 0 and 1.
🌐
Statology
statology.org › home › how to replace elements in numpy array (3 examples)
How to Replace Elements in NumPy Array (3 Examples)
March 30, 2025 - by Zach Bobbitt Last updated on Last updated on March 30, 2025 · You can use the following methods to replace elements in a NumPy array: ... #replace all elements greater than 8 or less than 6 with a new value of 20 my_array[(my_array > 8) | (my_array < 6)] = 20
🌐
Stringerssociety
stg.stringerssociety.com › home › news › replace values of a numpy index array with values of a list
Replace values of a numpy index array with values of a list - Modern Tech
August 17, 2026 - I'd like to replace values in an array, so that 1 is replaced by 0, and 2 by 10. ... I guess that's because I can't really write into a numpy array. P.S. The actual size of the numpy array is 514 by 504 and of the list is 8. ... import numpy as np a = np.array([1,2,2,1]).reshape(2,2) # palette must be given in sorted order palette = [1, 2] # key gives the new values you wish palette to be mapped to. key = np.array([0, 10]) index = np.digitize(a.ravel(), palette, right=True) print(key[index].reshape(a.shape))