The biggest problem with your code is that it's unreadable. Python code rule number one, if it's not readable, no one's gonna look at it for long enough to get any useful information out of it. Always use descriptive variable names. Almost didn't catch the bug in your code, let's see it again with good names, slow-motion replay style:

to_modify = [5,4,3,2,1,0]
indexes = [0,1,3,5]
replacements = [0,0,0,0]

for index in indexes:
    to_modify[indexes[index]] = replacements[index]
    # to_modify[indexes[index]]
    # indexes[index]
    # Yo dawg, I heard you liked indexes, so I put an index inside your indexes
    # so you can go out of bounds while you go out of bounds.

As is obvious when you use descriptive variable names, you're indexing the list of indexes with values from itself, which doesn't make sense in this case.

Also when iterating through 2 lists in parallel I like to use the zip function (or izip if you're worried about memory consumption, but I'm not one of those iteration purists). So try this instead.

for (index, replacement) in zip(indexes, replacements):
    to_modify[index] = replacement

If your problem is only working with lists of numbers then I'd say that @steabert has the answer you were looking for with that numpy stuff. However you can't use sequences or other variable-sized data types as elements of numpy arrays, so if your variable to_modify has anything like that in it, you're probably best off doing it with a for loop.

Answer from machine yearning on Stack Overflow
Top answer
1 of 9
57

The biggest problem with your code is that it's unreadable. Python code rule number one, if it's not readable, no one's gonna look at it for long enough to get any useful information out of it. Always use descriptive variable names. Almost didn't catch the bug in your code, let's see it again with good names, slow-motion replay style:

to_modify = [5,4,3,2,1,0]
indexes = [0,1,3,5]
replacements = [0,0,0,0]

for index in indexes:
    to_modify[indexes[index]] = replacements[index]
    # to_modify[indexes[index]]
    # indexes[index]
    # Yo dawg, I heard you liked indexes, so I put an index inside your indexes
    # so you can go out of bounds while you go out of bounds.

As is obvious when you use descriptive variable names, you're indexing the list of indexes with values from itself, which doesn't make sense in this case.

Also when iterating through 2 lists in parallel I like to use the zip function (or izip if you're worried about memory consumption, but I'm not one of those iteration purists). So try this instead.

for (index, replacement) in zip(indexes, replacements):
    to_modify[index] = replacement

If your problem is only working with lists of numbers then I'd say that @steabert has the answer you were looking for with that numpy stuff. However you can't use sequences or other variable-sized data types as elements of numpy arrays, so if your variable to_modify has anything like that in it, you're probably best off doing it with a for loop.

2 of 9
37

numpy has arrays that allow you to use other lists/arrays as indices:

import numpy
S=numpy.array(s)
S[a]=m
🌐
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) ...
Discussions

python - Replace values of an array based on a vector of indices - Stack Overflow
2 python - Replacing value in a numpy array's column given index from a vector with the same rows · 1 Replace values by indices of corresponding to another array More on stackoverflow.com
🌐 stackoverflow.com
python - How to replace certain elements of a NumPy array via an index array - Stack Overflow
I have the value of the new elements in a tuple/numpy array and the indexes of the elements of a that needs to be replaced in another tuple/numpy array. Below is an example of using python to do what I want.How do I do this efficiently in NumPy? More on stackoverflow.com
🌐 stackoverflow.com
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 - Getting and replace values from numpy array based on list of index - Stack Overflow
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 · 0 Numpy: Replacing part of array by another entire array based on selected index · Fix integral lower bound kerning ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python Guides
pythonguides.com › python-numpy-replace
Replace Values In NumPy Array By Index In Python
May 16, 2025 - Learn 4 efficient methods to replace values in NumPy arrays by index in Python. Master simple indexing, slicing, boolean indexing, and np.where() for data manipulation
🌐
Finxter
blog.finxter.com › how-to-replace-a-list-element-at-a-specific-index-in-python
How to Replace One or More List Elements at Specific Indices in Python? – Be on the Right Side of Change
March 8, 2021 - You can use the range() function to get the pair of the i-th index and the i-th replacement value in a for loop. Then, you replace all elements one-by-one. lst = ['Alice', 'Bob', 'Carl', 'Dave', 'Elena', 'Frank', 'George'] repl = ['None', 'Foo', 'Bar'] indices = [0, 2, 5] # Method 1: For Loop ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-replace-values-in-a-list-in-python
Replace Values in a List in Python - GeeksforGeeks
The rule checks the value and replaces ... the same. ... This method walks through the list using index positions and replaces values whenever the specified condition is met during iteration with the help of for loop....
Published: January 12, 2026
Find elsewhere
🌐
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 - Create a sample 2D array for demonstration purposes. Access the element using its row and column indices and assign a new value to replace a specific value. This replaces the value at the second row and second column with 10.
🌐
datagy
datagy.io › home › python posts › python: replace item in list (6 different ways)
Python: Replace Item in List (6 Different Ways) • datagy
August 12, 2022 - To learn more about Python list indexing, check out my in-depth overview here. If we want to replace a list item at a particular index, we can simply directly assign new values to those indices.
🌐
Python Guides
pythonguides.com › replace-items-in-a-python-list
Replace Items in a Python List (Index, Value, All)
March 5, 2025 - To replace items in a Python list, assign to an index: cities[1] = "Dallas". To replace by value, find the position with index() first, and replace every match with a list comprehension:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-update-in-array
Python - Update in Array - GeeksforGeeks
July 23, 2025 - The simplest way to update an array element is by accessing the element using its index and assigning a new value.
🌐
W3Schools
w3schools.com › python › python_lists_change.asp
Python - Change List Items
Change the second and third value by replacing it with one value: thislist = ["apple", "banana", "cherry"] thislist[1:3] = ["watermelon"] print(thislist) Try it Yourself » · To insert a new list item, without replacing any of the existing values, we can use the insert() method. The insert() method inserts an item at the specified index:
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-replace-values-in-a-list-in-python
How to Replace Values in a List in Python? - GeeksforGeeks
The simplest way to replace values in a list in Python is by using list indexing.
Published: January 4, 2025