Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

remove item from list while looping

l = ["a", "b", "c", "d", "e"]
for item in l[:]:
     if item == "b":
        l.remove(item)
 
print(l)
#the main trick of this problem is the traditional for loop will not work...it will give an "out of range" error saying
#this is because you are changing the list that you are for looping
#the traditional for loop will not work because you are changing the list it is iterating 
 
PREVIOUS NEXT
Tagged: #remove #item #list #looping
ADD COMMENT
Topic
Name
3+1 =