+ 6

Why the last '-1' isn't removed?

arr = [-1,-1,2,3,4,-1,6,7,8,9,-1] for i in arr: if i==-1: arr.remove(i) print(arr) After i print(arr)..i get >>[2, 3, 4, 6, 7, 8, 9, -1] My question is that, why the last '-1' isn't removed?

1st Apr 2020, 5:02 AM
Amit Dubey
Amit Dubey - avatar
19 odpowiedzi
+ 10
When you remove an item from a list, the counter in the next step jumps by one: At the first iteration i is -1 and you remove it. At the next iteration i is 2 (probably you think it should be -1, but as I said it jumps by one. Because the counter now should be on the second place of arr and now it's 2, and -1 is at the first place). It goes on until it reaches at the third -1. Then it removes the first occurrence of -1 which is at the first position but the current -1 remains unremoved and become the 4th element. Finally it reaches at the last -1 and when it wants to remove the first occurrence of -1, it's now on 4th position and the last -1 remains unremoved.
1st Apr 2020, 5:42 AM
Qasem
+ 11
Removing item while traversing the container is tricky. For that purpose you can remove -1 while the the container still contains a value -1. while arr.count(-1): arr.remove(-1)
1st Apr 2020, 5:39 AM
Ipang
+ 8
The reason why there are not all desired elements are removed, is because removing during an iteration on the list "changes" the indexes following a deletion position. But you can do the job like shown in the following samples # using reversed direction for iterating lst = [-1,4,3,-1,7,1,-1] for i in reversed(lst): if i == -1: lst.remove(i) print(lst) # using reversed direction for iterating lst = [-1,4,3,-1,7,1,-1] x = len(lst)-1 while x >= 0: if lst[x] == -1: del lst[x] x -= 1 print(lst) # using filter lst = [-1,4,3,-1,7,1,-1] print(list(filter(lambda x: x != -1, lst))) # using list comprehension lst = [-1,4,3,-1,7,1,-1] print([i for i in lst if i != -1])
3rd Apr 2020, 1:47 AM
Lothar
Lothar - avatar
+ 6
You can always see what the code does. Add print(arr) after "for i in arr:" and "arr.remove(i)". Another version of code may be: while -1 in arr: arr.remove(-1)
2nd Apr 2020, 12:13 PM
Александр Васильев
Александр Васильев - avatar
+ 4
The Golden Rule Never ever delete from a list that you are iterating.
2nd Apr 2020, 9:30 AM
Oma Falk
Oma Falk - avatar
+ 4
Loop over a copy of the iterable if there is any chance the iterable might change in size. arr = [-1, -1, 2, 3, 4, -1, 6, 7, 8, 9, -1] for i in arr[:]: # <-- this is a copy of "arr" if i == -1: arr.remove(i) print(arr)
2nd Apr 2020, 7:07 PM
rodwynnejones
rodwynnejones - avatar
+ 3
you might also use this arr[:] = (x for x in arr if x !=-1)
1st Apr 2020, 6:51 AM
John Robotane
John Robotane - avatar
+ 3
Tarun that's because of jumping counter in each iteration. For example: Arr=[0,1,2,3,4] For i in Arr: Arr.remove(i) Print(Arr) >>> [1,3]
1st Apr 2020, 8:04 PM
Qasem
+ 2
At this point it seems that EVERYTHING I HAVE LEARNT SO FAR IS WRONG. plz let me know the answer
1st Apr 2020, 7:46 PM
Tarun
Tarun - avatar
+ 2
Change the if condition to if -1 in arr: This way you can remove the -1 even if it is all over the list
2nd Apr 2020, 7:33 AM
amit avinoam
amit avinoam - avatar
+ 1
Amit Dubey you can't delete the last item by the structure you've mentioned. The easiest is what Ipang has said.
1st Apr 2020, 5:57 AM
Qasem
+ 1
The reply with a 'green tick' is the reason behind it...Tarun
1st Apr 2020, 7:49 PM
Amit Dubey
Amit Dubey - avatar
+ 1
I have altered the expression and found that i is not iterating to last of the list. arr=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1] for i in arr: If i==-1: arr.remove(i) print(i) Excecute this code and u ll find lots of -1 Although you can simply remove this problem by i+=1 but still unable to find whats going on
1st Apr 2020, 7:58 PM
Tarun
Tarun - avatar
+ 1
I Need debugging.
1st Apr 2020, 8:13 PM
Tarun
Tarun - avatar
+ 1
amit avinoam Thanks for telling me that, but i just wanted to know why it doesn't work..
2nd Apr 2020, 7:41 AM
Amit Dubey
Amit Dubey - avatar
0
Qasem so, should i decrease the counter by 1 as to get it removed?
1st Apr 2020, 5:46 AM
Amit Dubey
Amit Dubey - avatar
0
finally i got it why this is happening.... it took me three days to figure out but it was a lesson worth learning.
2nd Apr 2020, 12:15 PM
Tarun
Tarun - avatar
- 1
Use del a[-1] to remove last element
2nd Apr 2020, 4:33 PM
Het Fadia
Het Fadia - avatar
- 2
it should be position i so for your if statement it sould be if arr[i] == 1: arr.remove(i)
3rd Apr 2020, 3:53 AM
Ethan Decker
Ethan Decker - avatar