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
🌐
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) ...
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
Discussions

python - Replace value in an array by its index in a list - Stack Overflow
In a given array I want to replace the values by the index of this value in an other array (which doesn't contain duplicates). Here is a simple example of I'm trying to do: import numpy as np from... More on stackoverflow.com
🌐 stackoverflow.com
October 26, 2015
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
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 - 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
People also ask

How do I replace one item in a Python list?
Assign to its zero-based index, such as items[index] = new_value; an invalid index raises IndexError.
🌐
pythonpool.com
pythonpool.com › home › tutorials › replace items in a python list: index, loop, map(), and slices
Replace Items in a Python List: Index, Loop, map(), and Slices
How do I replace every matching item in a list?
Use enumerate() and assign matching indexes in place, or build a new list with a conditional comprehension.
🌐
pythonpool.com
pythonpool.com › home › tutorials › replace items in a python list: index, loop, map(), and slices
Replace Items in a Python List: Index, Loop, map(), and Slices
Can I replace list items with map()?
Yes. Pass a named transformation to map() and convert the lazy iterator to list when a materialized result is needed.
🌐
pythonpool.com
pythonpool.com › home › tutorials › replace items in a python list: index, loop, map(), and slices
Replace Items in a Python List: Index, Loop, map(), and Slices
🌐
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 ...
Find elsewhere
🌐
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
🌐
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.
🌐
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.
🌐
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:
🌐
Career Karma
careerkarma.com › blog › python › replace item in list in python: a complete guide
Replace Item in List in Python: A Complete Guide: A Complete Guide
December 1, 2023 - You can replace an item in a Python list using a for loop, list indexing, or a list comprehension. The first two methods modify an existing list whereas a list comprehension creates a new list with the specified changes.