+ 8
[ASSIGNMENT] What is the output of this code and why?
a=[1,2,3,4,5] for n in a : a.remove(n) print (a)
5 Answers
+ 10
In the for loop :
0) In 1st iteration, n becomes 1(the first element), and a.remove(1) removes the element 1 from the list.
Thus, a becomes [2,3,4,5].
1) In 2nd iteration, n becomes second element of the list (3 in this case), and a.remove(3) removes the element 3.
Thus, a becomes [2,4,5].
2) In 3rd iteration, n becomes third element of the list (5 in this case) and the 5 gets removed.
Thus a becomes [2,4].
Now, after this iteration, we reach the end of the list, and so the loop terminates.
Thus, we get the output : [2,4].
+ 5
thank you so much to all of you
+ 4
Jan Markus
It seems a.remove(n) removes the first element equal to n, and maybe that is why the output is similar in the list of strings.
+ 3
you can do it: https://code.sololearn.com/cIMO67rOjogA/?ref=app
- 1
Hey randeep actually i need your help plz WhatsApp me 9518294243