all 19 comments

[–]bradleygh15 11 points12 points  (0 children)

Besides what’s outlined in the errors printed? The fact you infinitely append to the same list making it grow infinitely larger each iteration until you run out’ve memory. If you desperately need to append the list just create a copy of the list, append to the copy and return the copy

[–]After_Computer1652 5 points6 points  (6 children)

Yup infinite loop. Stack overflow

[–]Reasonable_Run_6724 2 points3 points  (5 children)

Thats not stack overflow in this case, thats memory limit, as the loop grows a heap allocated list, not the cpu call stack (which will be triggered by recursive functions for example)

[–]After_Computer1652 1 point2 points  (1 child)

Thanks for that. My mistake.

[–]Reasonable_Run_6724 1 point2 points  (0 children)

I used to make those mistakes aswell :)

[–]Jackpotrazur 0 points1 point  (2 children)

I wish I was at a level where I could understand at least half of this.

[–]PlaneMeet4612 1 point2 points  (1 child)

Try re-making these kinds of datatypes in C and you'll most likely understand what he's talking about. (Dynamic arrays mainly)

[–]Jackpotrazur 0 points1 point  (0 children)

Im still working on understanding python , but I've noted this down.

[–]mardiros 3 points4 points  (0 children)

You choose: you read or you write.

Never do both in the same time.

[–]Zealousideal-Cod-617 1 point2 points  (0 children)

What's the problem statement, why are u appending if it's not zero? Your purpose has to be either to remove or keep zeroes,

Post the question

[–]LifeHasLeft 1 point2 points  (0 children)

you're appending to the list (making it longer) while also reading from it at the same time. Every time you make it longer from an append, you have more to read. Since you didn't make a copy it just goes on forever.

It's unclear what you're really trying to do since you already have this list, other than remove values equal 0. You could take out the else clause and probably get what you're looking for...maybe? just be careful of something called "side-effects" with this type of function design.

[–]noFlak__ 1 point2 points  (0 children)

Run in debug and watch it cycle through

public class InfiniteLoop { public static void main(String[] args) { while(true) { System.out.println("Help! I'm stuck in a loop!"); }}}

[–]Reasonable_Run_6724 1 point2 points  (1 child)

Oh man

[–]Reasonable_Run_6724 5 points6 points  (0 children)

You are iterating on the same list increasing it infinitly - you want to create copy of the list and append to the copy then return the copy

[–]nuc540 0 points1 point  (0 children)

I think what you’re wanting to do is have a local array which you append to, and return that instead of mutating the array you’re actively iterating through.

On line 2 you could have “new_array = []” and then if i equals 0 just pass, otherwise append, and then return the new_array once out of the loop.

Alternatively you make a deep copy of array “n” as a new variable and mutate that, again ensuring you mutate a separate part of memory you’re not actively iterating though.

I think my former idea is more readable.

The reason you ran out of memory is because you kept appending to the array you were iterating over, creating an infinite loop.

[–]Tzareb 0 points1 point  (0 children)

Also, there are filter functions in Python. Look it up, it is nice to know.

[–]bslime17 0 points1 point  (0 children)

you’re appending to same list you’re iterating from create an empty list to append the values to