0

l=[1,2,3,2,2,4,5,2,5,2,4] x=2 i=0 while i<l.count(x): if x in l: l.remove(x) i=i+1 print(l)

Please explain flow of this question and output

24th Jul 2022, 10:39 AM
Irshad Ahmad
Irshad Ahmad - avatar
2 Answers
+ 2
Irshad Ahmad count function will count the value exist in list So l.count(2) = 4 remove function will remove value from the list but single value not all duplicate value remove(x) #here value x is a value. x in l: here checking value x exist in list or not. As we are removing value from list "l" so after every iteration value will be remove from the list and you will get new list. while 0 < 4: //true if 2 in l: //true l.remove(2) Loop will work untill the condition returns True and after each iteration value will be remove on particular index and new list will be generate. 1st iteration condition 0 < 5 is true and 2 exist in list so l.remove(2) = [1, 3, 2, 2, 4, 5, 2, 5, 2, 4] #new list 2nd iteration 1 < 4: true and 2 exist in new list so l.remove(2) = [1, 3, 2, 4, 5, 2, 5, 2, 4] #new list 3rd iteration 2 < 3: true and 2 exist in new list so l.remove(2) = [1, 3, 4, 5, 2, 5, 2, 4] #new list 4th iteration 3 < 2: false so loop will be break And new list is [1, 3, 4, 5, 2, 5, 2, 4]
24th Jul 2022, 11:12 AM
A͢J
A͢J - avatar
+ 1
Thanks bro got it❤️❤️❤️❤️
24th Jul 2022, 11:15 AM
Irshad Ahmad
Irshad Ahmad - avatar