r/PythonLearning • u/Wtorrell • Oct 05 '25
Help Request Don’t know why code isnt working
For school, I have been assigned a task to create a code that iterates through the 1d array and removes any integers below 0 and the string ‘placeholder’. However, I can’t seem to get it to work. Any help would be much appreciated.
33
u/localghost Oct 05 '25
It's a bad idea to modify the very collection of items you're currently looping through.
1
8
u/MonochromeDinosaur Oct 05 '25
You should almost never take actions that change the size of the list (add/remove) as you’re iterating it.
You’re changing the length of it as the loop is happening and you end up with errors like this one.
Raymond Hettinger (Python core contributor) said it best: ”If you are mutating something while you are iterating over it, you are living in a state of sin and you deserve whatever happens to you"
3
5
u/bobbybridges Oct 05 '25
You're modifying the list length during the loop which causes it to be shorter than the length you are trying to iterate on. I suggest making an empty list and filling it with the values you are NOT removing instead
2
1
2
u/tinySparkOf_Chaos Oct 05 '25
Removing something from a list changes the indexing of the list.
[A,B,C]
Remove B
[A, C]
Index 1 is now C, if you ask for index 2 you get an error.
You can avoid this a couple of ways.
Running the index backwards. Start the loop from the largest number and count to 0.
store the locations of bad items, remove them after completing the loop.
use a while loop with a counter. Don't advance the counter for the next loop if you remove.
Also, "for item in list" or "for i, item in enumerate(list)" is easier than "for i in range(length(list)), item = list(i)
1
u/gra_Vi_ty Oct 05 '25
Hello brother the problem is indexerror,this happend because each time you if condition becomes true it removes an element right?that's why your list length gets smaller so,your range for loop is the for the initial list with 7, elements so that why this shows indexerror ,try iteration without using index
1
u/gra_Vi_ty Oct 05 '25
Try this: For i in user_data: #here it takes each element of list outside rather than using index this is better For i in user_data: if (type(i)==int and i>0) or i=='placeholder': user_data.remove(i)
1
u/Jaded_Pipe_7784 Oct 05 '25
This will not work for exactly the same reason the original solution op provided doesn't work
2
u/willis81808 Oct 05 '25
Not quite. It will not work, but for a slightly different reason.
This suggestion will not result in index errors like OP's code does, but will result in skipped values. The actual output of this suggestion is:
['a_user', -1, 'placeholder', 15, -4]The real answer is to either iterate over the list in reverse, iterate over a shallow copy, or use list comprehension.
Shallow copy example:
user_data = ["a_user", -1, 56, "placeholder", 90, 15, -4] for value in user_data[:]: if (isinstance(value, int) and value <= 0) or value == "placeholder": user_data.remove(value) print(user_data) # Output: ['a_user', 56, 90, 15]Iterating in reverse example:
user_data = ["a_user", -1, 56, "placeholder", 90, 15, -4] for i in range(len(user_data) - 1, -1, -1): value = user_data[i] if (isinstance(value, int) and value <= 0) or value == "placeholder": user_data.remove(value) print(user_data) # Output: ['a_user', 56, 90, 15]List comprehension example:
user_data = ["a_user", -1, 56, "placeholder", 90, 15, -4] user_data = [ value for value in user_data if not ((isinstance(value, int) and value <= 0) or value == "placeholder") ] print(user_data) # Output: ['a_user', 56, 90, 15]
1
1
u/gobelgobel Oct 06 '25
The others have made correct recommendations: You're altering the very object you're looping over.
Another suggestion: Avoid looping over indices of a collection (in this case a list) if you don't need the index: "for i in range(len(items))"
Here you definitely don't. If you do need the index, use
for i, item in enumerate(items):
# here use the index i and the itemAnd be as descriptive with the variable naming as you can. If it's many use plural, just one, singular. Makes your code way more readable.
for item in items:
# here do something with the item0
u/SCD_minecraft Oct 05 '25
a = [1, 2, 3, 4, 5]
b = len(a) #5
a[b] #IndexError
a[b-1] #5Lists start at index 0
0
u/aceinet Oct 06 '25 edited Oct 06 '25
You need to do i -= 1 after the remove() in the condition. This way the loop jumps one element, not two. But keep in mind, mutating an array in a loop iterating through that array is a bad idea. Every language that I know (C++, java, python (for in)) throws an error if you would do something like that
15
u/PriorTrick Oct 05 '25
Aside from mutating the array during iteration, like others have mentioned. You need to type check using isinstance(user_index[i], int), right now you are checking if the value equals int, when it’s of type(int).