I found this in the docs: https://docs.python.org/3/tutorial/controlflow.html#for

Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy.

I was wrong in my first response, when iterating through a list it returns the actual items in that list. However, it seems they cannot be edited directly while they are being iterated through. This is why iterating through the integers the length of the list works.

As for why the .reverse() function works, I think it's because it is affecting a list instead of a value. I tried to use similar built in functions on nonlist datatypes like .replace() on strings and it had no effect.

All of the other list functions I tried worked: .append(), .remove(), and .reverse() as you showed. I'm not sure why this is, but I hope it clears up what you can do in for loops a bit more.

Answer to old question below:

The way you are using the for loops doesn't affect the actual list, just the temporary variable that is iterating through the list. There are a few ways you can fix this. Instead of iterating through each element you can can count up to the length of the list and modify the list directly.

c = [1,2,3]
for n in range(len(c)):
    c[n] += 3
print(c) 

You can also use the enumerate() function to iterate through both a counter and list items.

c = [1,2,3]
for n, x in enumerate(c):
    c[n] = x + 3
print(c) 

In this case, n is a counter and x is the item in the list.

Finally, you can use list comprehension to generate a new list with desired differences in one line.

c = [1, 2, 3]
d = [x + 3 for x in c]
print(d)
Answer from Aeolus on Stack Overflow
Top answer
1 of 4
25

I found this in the docs: https://docs.python.org/3/tutorial/controlflow.html#for

Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy.

I was wrong in my first response, when iterating through a list it returns the actual items in that list. However, it seems they cannot be edited directly while they are being iterated through. This is why iterating through the integers the length of the list works.

As for why the .reverse() function works, I think it's because it is affecting a list instead of a value. I tried to use similar built in functions on nonlist datatypes like .replace() on strings and it had no effect.

All of the other list functions I tried worked: .append(), .remove(), and .reverse() as you showed. I'm not sure why this is, but I hope it clears up what you can do in for loops a bit more.

Answer to old question below:

The way you are using the for loops doesn't affect the actual list, just the temporary variable that is iterating through the list. There are a few ways you can fix this. Instead of iterating through each element you can can count up to the length of the list and modify the list directly.

c = [1,2,3]
for n in range(len(c)):
    c[n] += 3
print(c) 

You can also use the enumerate() function to iterate through both a counter and list items.

c = [1,2,3]
for n, x in enumerate(c):
    c[n] = x + 3
print(c) 

In this case, n is a counter and x is the item in the list.

Finally, you can use list comprehension to generate a new list with desired differences in one line.

c = [1, 2, 3]
d = [x + 3 for x in c]
print(d)
2 of 4
10

If you want to keep it consice without creating an additional variable, you could also do:

c = [1,2,3] 
print(id(c)) 
c[:] = [i+3 for i in c] 
print(c, id(c))

Output:

2881750110600
[4, 5, 6] 2881750110600
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-change-list-item
Python - Change List Item - GeeksforGeeks
July 23, 2025 - For each element: If the element ... If we want to modify elements in place a for loop with enumerate() allows us to iterate through the list while keeping track of indices....
Discussions

python - Modify List In-Place - Stack Overflow
I want to modify the list within a function, that will be later reflected onto the outer world so that it will change whenever we call a function. Yet, the list is unmodified. def modify_in_place( More on stackoverflow.com
🌐 stackoverflow.com
How do I "modify s in place"? I tried directly overwriting it(see stdout), but leetcode still doest recognize it. Total beginner.
Two pointers, and tuple assignment. You don’t need temp in Python for most things. More on reddit.com
🌐 r/leetcode
16
12
May 15, 2021
In-place solution
A simple coding question. I was wondering if my solution is “in-place”? Move Zeroes Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. More on discuss.python.org
🌐 discuss.python.org
7
0
September 7, 2022
python - How to modify a list in place - Stack Overflow
31 Modification of the list items in the loop (python) More on stackoverflow.com
🌐 stackoverflow.com
🌐
Compciv
compciv.org › guides › python › fundamentals › lists-mutability
Lists, mutability, and in-place methods | Computational Methods in the Civic Sphere at Stanford University
First of all, I think it's worth looking at the tuple object, which is similar to a list and frequently used in Python programs. If you understand lists, you understand tuples. The main difference is that tuples are immutable. After seeing examples of immutable objects, you might appreciate better what it means when we say that a list is mutable, and that its in-place methods "mutate" its contents.
🌐
W3Schools
w3schools.com › python › python_lists_change.asp
Python - Change List Items
To change the value of a specific item, refer to the index number.
🌐
SitePoint
sitepoint.com › python hub › change list items
Python - Change List Items | SitePoint — SitePoint
The simplest way to modify a list element is to use its index and the assignment operator =. ... Here, we replaced the element at index 1 (the second element, since indexing starts at 0) from "banana" to "orange".
Find elsewhere
🌐
APXML
apxml.com › courses › python-for-beginners › chapter-4-organizing-data-collections › python-modifying-lists
Modifying Python Lists | Add, Remove, Change Items
This modifies the list in place, making it empty. The list variable still exists, it just contains no elements. Visual representation of list modifications starting with ['A', 'B', 'C']. Each step shows the result of applying a modification method. Understanding these methods for changing, adding, and removing items provides you with the tools needed to effectively manage dynamic collections of data using Python ...
🌐
Python.org
discuss.python.org › python help
In-place solution - Python Help - Discussions on Python.org
September 7, 2022 - A simple coding question. I was wondering if my solution is “in-place”? Move Zeroes Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array.
🌐
Built In
builtin.com › data-science › python-list
Python Lists and List Manipulation Tutorial | Built In
Modifying a list with the count method. | Image: Michael Galarnyk · Sort a Python List. the actual code would be: z.sort(). | Image: Michael Galarnyk · The sort method sorts and alters the original list in place.
🌐
Nkantar
nkantar.com › blog › 2017 › 10 › modifying-a-list-in-place-in-python
Modifying a List in Place in Python | Blog | Nik Kantar
October 5, 2017 - >>> my_list = list_copy = list(range(5)) # [0, 1, 2, 3, 4] >>> for element in list_copy: # again note what we're iterating through ... my_list.remove(element) ...
🌐
Dummies
dummies.com › article › technology › programming-web-design › python › how-to-modify-lists-in-python-203522
How to Modify Lists in Python | dummies
January 25, 2017 - Python has added another element to List1. However, using the insert() function lets you add the new element before the first element. The new list, List2, is a precise copy of List1. Copying is often used to create a temporary version of an existing list so that a user can make temporary modifications to it rather than to the original list.
Author:
🌐
Matt Bognar
homepage.divms.uiowa.edu › ~sriram › 16 › spring11 › 22C16Mar23.pdf pdf
M A R C H 2 3 Operations that modify Lists in Place
Operations that modify Lists in · Place · Lists and strings also have important differences · In Python some data types are mutable, i.e., they can be modified in · place. Of the data types we have seen so far, e.g., int, long, float, bool, str, and list, only list is mutable.
🌐
Data to Fish
datatofish.com › modify-list-python
How to Modify an Item Within a List in Python
z = [e0, e1, e3] # append a an element z.append(e4) # delete an element (y) z.pop(index_y) # replace a single element z[index_y] = new_value # replace all occurences of a value using list comprehension z = [new_value if x == y else x for x in z] Suppose, you want to append mackerel to list of fishes: fish = ['salmon', 'pufferfish', 'shark'] fish.append('mackerel') print(fish) ... Suppose, you want to change the first element (salmon) of the fish list to mackerel.
🌐
LabEx
labex.io › tutorials › python-how-to-modify-python-list-elements-efficiently-466999
How to modify Python list elements efficiently | LabEx
Preallocate list size when possible · Use generators for memory-intensive operations · Prefer in-place modifications · Leverage functional programming techniques · Benchmark and profile your code · By mastering these Python list modification techniques, developers can significantly improve their programming efficiency and code quality.
🌐
Quora
quora.com › Whats-the-fastest-way-to-modify-a-list-in-python
What's the fastest way to modify a list in python? - Quora
Edit2: These seem to match up with ... http://wiki.python.org/moin/TimeComplexity ... "Fastest" depends on what "modify" means (append, insert, delete, slice-replace, map-in-place) and what performance measure you care about (amortized time, worst-case time, vectorized throughput). Below are practical patterns and their performance characteristics for CPython lists (dynamic arrays), ...
🌐
Ponderings of an Andy
andrewwegner.com › home › categories › technical solutions
Python Gotcha: Modifying a list while iterating - Andrew Wegner
January 3, 2024 - numbers_list = [0, 9, 2, 1, 5, 1, 10, 4, 3] numbers_list = [nums for nums in numbers_list if nums >= 5] print(numbers_list)