+ 1

Why is the output of this code in such manner?

mylist = [i for i in range(8)] for i in mylist: mylist.remove(i) print(sum(mylist))

3rd Feb 2022, 10:42 PM
cryptic blake
cryptic blake - avatar
9 Answers
0
ravilnicki I do use print to to assess and check my code and its output all the time And if I understand your explanation well, in every iteration, remove() method deals with the index of items in a list rather than the item itself, and also remembers the index of where it stopped to carry on to the next?
3rd Feb 2022, 11:40 PM
cryptic blake
cryptic blake - avatar
0
ravilnicki when you say first occurrence, what does it mean? 'Cause obviously the list doesn't contain 2 or more occurrences of the same character. Kindly elaborate pls 'cause it really seems remove is trying its best not to remove an item in same index as before
3rd Feb 2022, 11:54 PM
cryptic blake
cryptic blake - avatar
0
ravilnicki But there is only occurrence of each character in the list... I kinda need an explicit answer to quench my thirst for one lol
4th Feb 2022, 12:00 AM
cryptic blake
cryptic blake - avatar
0
ravilnicki sorry if I'm bothering you, I think I get it now. for loop checks every item at a particular index once as you've stated...so remove just does its own job of removing first occurrences. I realized my mistake now. Thanks for your assistance šŸ™‚šŸ¤
4th Feb 2022, 12:18 AM
cryptic blake
cryptic blake - avatar