32

Note to self:
Don't try to remove elements from a list using a for loop because the FUCKING LIST SIZE CHANGES!
Just copy required elements to another list and discard the previous.
Spent fucking 2 hours on this.
/Rant

Comments
  • 1
    What was the context of that? What were you trying to do?
  • 1
    @densedever basically i had a list of elements. Some elements at every 2i +1 position were repeats. So i did x.remove(2i+1). But i got array out of bounds. I completely forgot that after every removal, the array shrinks and 2i+1 is not valid anymore.
    This is python.
    FML
  • 2
    Why not just do set(mylist)?
  • 2
    @densedever sorry. I made a mistake. In my hurry, I didn't describe my problem properly. Elements were not repeats but rather redundant. But yes, your tip might come handy later.
  • 10
    You can use a for loop, but instead of counting up an array, count down.
    Ex:

    for(var i = array.length; i > 0; i--){
    //Code here
    }
  • 7
    filter using lambda function
  • 1
    Use mutexes to make sure no one touches the list until ur finished
  • 2
    Mutability by default is a bitch
  • 0
    I second the filter function with a lambda approach.

    Although, if the data is redundant rather than duplicated and the position of the data in the list is what determines if it is redundant then the condition the lambda function checks for might be tricky to write and a reverse for loop might be the quickest route.
Add Comment