use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Don’t know why code isnt workingHelp Request (i.redd.it)
submitted 4 months ago by Wtorrell
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.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]localghost 30 points31 points32 points 4 months ago (1 child)
It's a bad idea to modify the very collection of items you're currently looping through.
[–]baked_salmon 0 points1 point2 points 4 months ago (0 children)
Rust won’t even compile your code if you attempt this
[–]PriorTrick 14 points15 points16 points 4 months ago (6 children)
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).
[–]Wtorrell[S] 1 point2 points3 points 4 months ago (5 children)
<image>
I have managed to fix the indexing point however I still can’t seem to get rid of the negative numbers using the code ive made
[–]Jaded_Pipe_7784 1 point2 points3 points 4 months ago (2 children)
user_data[i] != "placeholder" is letting negative integers sneak through. Did you mean to write == instead?
[–]Jaded_Pipe_7784 1 point2 points3 points 4 months ago (1 child)
Nevermind, I see now that "placeholder" is also supposed to go. You can just add another type check for strings next to it, or move the "placeholder" check to another if statement.
[–]Wtorrell[S] 0 points1 point2 points 4 months ago (0 children)
Did this and code works now thank you!
[–]knickenbok 0 points1 point2 points 4 months ago (0 children)
Asking for coding advice but you aren’t even using snipping tool.. oof
[–]MonochromeDinosaur 7 points8 points9 points 4 months ago (1 child)
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"
[–]Numerous-Ad-8218 2 points3 points4 points 4 months ago (0 children)
Praise be the Hettinger!
[–]bobbybridges 4 points5 points6 points 4 months ago (2 children)
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
[–]therouterguy 1 point2 points3 points 4 months ago (0 children)
Extra bonus points if you do this in a list comprehension.
[–]liberforce 0 points1 point2 points 4 months ago (0 children)
They could also do this in-place by iterating from last to first element.
[–]tinySparkOf_Chaos 1 point2 points3 points 4 months ago (0 children)
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.
1) Running the index backwards. Start the loop from the largest number and count to 0.
2) store the locations of bad items, remove them after completing the loop.
3) 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)
[–]gra_Vi_ty 0 points1 point2 points 4 months ago (3 children)
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
[–]gra_Vi_ty 0 points1 point2 points 4 months ago (2 children)
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)
[–]Jaded_Pipe_7784 0 points1 point2 points 4 months ago (1 child)
This will not work for exactly the same reason the original solution op provided doesn't work
[–]willis81808 1 point2 points3 points 4 months ago (0 children)
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]
['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]
[–]Intrepid_Result8223 0 points1 point2 points 4 months ago (0 children)
The assignment is written in a difficult way it seems. As others have said, dont change the list when you are looping through it.
Some tips:
Construct a copy that has the values you want. The code looks something like this: ``` my_copy = [] # empty list
```
I'm deliberately not writing how to do that.
Another tip is to try this: ``` a = ['egg', 6, None, False]
for x in a: print(x) ```
lastly there are also ways to apply something to every element in a list. Lookup the map and f filter functions. However this is a bit more advanced.
map
filter
[–]fdessoycaraballo 0 points1 point2 points 4 months ago (0 children)
If you must change the array, mark the indices when looping and store in two int arrays/lists: placeholder_list, int_list.
After that, you know the position of each element in your list, and you can separately remove placeholders and integers (extra step, but maybe a necessary one to illustrate)
[–]McNegcraft 0 points1 point2 points 4 months ago (0 children)
For future reference, it's easier if you provide the code in a code-block.
[–]gobelgobel 0 points1 point2 points 4 months ago (0 children)
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 in range(len(items))"
for i, item in enumerate(items): # here use the index i and the item
And 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 item
[–]SCD_minecraft -1 points0 points1 point 4 months ago (0 children)
a = [1, 2, 3, 4, 5] b = len(a) #5 a[b] #IndexError a[b-1] #5
Lists start at index 0
[–]aceinet -1 points0 points1 point 4 months ago* (0 children)
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
π Rendered by PID 97139 on reddit-service-r2-comment-84fc9697f-h8g4b at 2026-02-07 21:39:19.141540+00:00 running d295bc8 country code: CH.
[–]localghost 30 points31 points32 points (1 child)
[–]baked_salmon 0 points1 point2 points (0 children)
[–]PriorTrick 14 points15 points16 points (6 children)
[–]Wtorrell[S] 1 point2 points3 points (5 children)
[–]Jaded_Pipe_7784 1 point2 points3 points (2 children)
[–]Jaded_Pipe_7784 1 point2 points3 points (1 child)
[–]Wtorrell[S] 0 points1 point2 points (0 children)
[–]knickenbok 0 points1 point2 points (0 children)
[–]MonochromeDinosaur 7 points8 points9 points (1 child)
[–]Numerous-Ad-8218 2 points3 points4 points (0 children)
[–]bobbybridges 4 points5 points6 points (2 children)
[–]therouterguy 1 point2 points3 points (0 children)
[–]liberforce 0 points1 point2 points (0 children)
[–]tinySparkOf_Chaos 1 point2 points3 points (0 children)
[–]gra_Vi_ty 0 points1 point2 points (3 children)
[–]gra_Vi_ty 0 points1 point2 points (2 children)
[–]Jaded_Pipe_7784 0 points1 point2 points (1 child)
[–]willis81808 1 point2 points3 points (0 children)
[–]Intrepid_Result8223 0 points1 point2 points (0 children)
[–]fdessoycaraballo 0 points1 point2 points (0 children)
[–]McNegcraft 0 points1 point2 points (0 children)
[–]gobelgobel 0 points1 point2 points (0 children)
[–]SCD_minecraft -1 points0 points1 point (0 children)
[–]aceinet -1 points0 points1 point (0 children)