a=[1,2,3,4,5] for i in a: a.remove(i) print(a) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

a=[1,2,3,4,5] for i in a: a.remove(i) print(a)

Why all the elements aren't removed from the list. I'm getting [2,4] as output

22nd May 2020, 6:20 PM
Levi
Levi - avatar
15 Answers
+ 1
Let me put this in a big picture. remove () removes the first occurrence of the given object . For first iteration i=1 removes 1 from the list and decrease its size by 1 so you get [2,3,4,5] Note the shift of indices For second iteration i=3(since index 0 is already processed) removes 3 from the list and decrease its size by 1 a=[2,4,5] in the last iteration i=5 removes 5 and you get [2,4]
24th May 2020, 6:58 PM
Hima
Hima - avatar
+ 6
Imagine an internal for-loop, which goes through each index of your array. So it uses indexes 0, 1, 2, 3, 4 for the steps. Index 0: -> remove value 1 a = [2, 3, 4, 5] Index 1: -> remove value 3 a = [2, 4, 5] Index 2: -> remove value 5 a = [2, 4] Index 3: -> loop breaks, because index is out of range
22nd May 2020, 6:32 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 4
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 is right only pop() method removes the element from the given position. I too don't understand the explanation of Manu_1-9-8-5 To remove all elements in a list. You can try this... a = [1,2,3,4,5] for i in range(5): a.pop() print(a) (or) As rodwynnejones and Oma Falk explained. You can try this too... a = [1, 2, 3, 4, 5] for i in a[:]: a.remove(i) print(a)
24th May 2020, 1:27 AM
Jenson Y
+ 3
a = [1,2,3,4,5] for i in range(5): a.remove(i+1) print(a)
22nd May 2020, 6:29 PM
fps
fps - avatar
+ 3
Lay_in_life I never said that remove would do that. I just tried to answer the initial question why not all elements aren't removed and why the output is [2, 4]. And with the iterator example you can look at each step. I'm pretty sure that every one of us knows a better way to get an empty list.
24th May 2020, 6:59 AM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 2
This should work.... 👆🏾👆🏾👆🏾
22nd May 2020, 6:29 PM
fps
fps - avatar
+ 2
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Why do you see a difference in removing by value or by index? No matter which you use, it has no direct dependency to the loop.
22nd May 2020, 6:43 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 2
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 No, it says Index 0: -> remove value 1 Here with your example a-e and printing each loop element. https://code.sololearn.com/c2epoR297SB8/?ref=app
22nd May 2020, 6:51 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 1
When you modify the length of an iterable in a loop....always loop over a copy of the iterable e.g:- a = [1, 2, 3, 4, 5] for i in a[:]: # <--.note the [:] a.remove(i) print(a)
22nd May 2020, 6:45 PM
rodwynnejones
rodwynnejones - avatar
+ 1
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 your example is not reflecting what I'm saying. I say remove value, not index. So you pass "a", not "1".
22nd May 2020, 6:46 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 1
a=[1,2,3,4,5] for i in a[:] : a.remove(i) print(a) should work
22nd May 2020, 7:05 PM
Oma Falk
Oma Falk - avatar
+ 1
I created an example using the iterator of array (list_iterator). Once without and once with removing items. For each step the current element value is printed as well as the iterator. You can see the current array state and a number, which I hardly assume to be the next index (as it is zero initially). https://code.sololearn.com/ct4YGUvR7PBL/?ref=app
22nd May 2020, 7:59 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
0
Use clear function
24th May 2020, 2:08 PM
Ayush Singh
Ayush Singh - avatar
0
@Prince Kumar (for i in a:) this variable i takes all element in "a" by its index and stores in it.You should not pass the index position in remove function. You should pass the element which you want to remove.Read the code carefully. a=[1,2,3,4,5] for i in a: a.remove(i) print(a) For first iteration i takes the 0th index position element # Note that it takes 0th index position element not index Then in remove function it passes that element to remove . After removing the element you can see the list as [2,3,4,5] Now for second iteration the iterating variable i takes 1th index position element as 3 and it removes and so on.. Thank You
15th Apr 2022, 1:39 AM
muthupandi deivamsanmugam
0
a=[3,6,8,9,6,0,7] Remove 6 without using sequence function
14th May 2022, 11:35 AM
Santosh Maurya