Python Challenge Question 💬 | SoloLearn: Learn to code for FREE!


Python Challenge Question 💬

What is the output of this code? myList = [i for i in range(8)] for i in myList: myList.remove(i) print(sum(myList)) Can anyone explain me how the answer is 16?

8/29/2020 4:00:03 PM

No Name 👽

55 Answers

New Answer


it must be 16 simple don't think that it was goinig according to entities, instead it goes according to indexing in reality So, it will remove 0 first as it will check index 0 first then 1 will come at index 0 but now it was not going to check again for index0 ; it will check for index 1 and now at index 1 there is 2 so it will remove it so on .... finally, myList contain [1, 3, 5, 7] and it's sum will be 16


You are iterating and changing myList at the same time. That's why it's giving us unexpected output. After every iteration in the for loop. myList is not the same. See this code to visualize


Akash Chaudhary don't use abusive words. also remove your comments ASAP.


Ahli Pengobatan Alat Vital hello Don't spam. You're in the wrong place.


It's not removing even numbers... It removing at indexes 0,1,2,3 there we have values coming 2,4, 6, 8 So after every removal list is changing,, for i in list: Initially I = 0,1,2,3,4,.. And List is 0,1,2,3,4,5,6,7 At index 0, removing at 0, removes value 0. Now list is 1 2 3,4 5 6 7 Index at 1 removing, see list[1] is 2, removing that, list will becomes 1,3,4,5,6,7 Index at 2, removing i.e list[2] is 4 so after removing list is 1,3,5,6,7 Index at 3, removed I.e list[3] is 6 so then, list is 1,3,5,7 No value at index 4 now... So sum is 16


To avoid this problem as well described by Terminal_Phantom and Jayakrishna🇮🇳 , these kind of algorithms can be used: - iterate over the list not from the beginning up to the end, but by starting at the end up to the beginning or - iterating over a copy of the list, which will not be reduced by removing


Hardik Rajubhai Poriya & विशेष Thank you both 😊


♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ I thought you would understand it, like others 🙄


Sanjay Kamath Thank you 😊


Jayakrishna🇮🇳, Terminal_Phantom & Lothar Thanks again for taking your time I'm clear now 🌻🤗


vamsi Thank you too 😊


Ahmed Kamal read the answers one by one. Your doubt will be clear 🙂


Terminal_Phantom & Rohit Rajendra Brahmankar yes, I know it. Can you please tell me the reason?


♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ I can see the answer below the result too 🤷🏻‍♀️ But I need explanation 🤦🏻‍♀️


Like this List 01234567 Index 0 List 1234567 Index 1 List 134567 Index 2 List 13567 Index 3 List 1357 Output 1357 🤠


Akash Chaudhary I'm also a beginner, like you. I suggest you to ask the same to any mods or experienced users. They can help you 🙂


Sanjay Kamath Yes, it helped me 😊


Julio Lopez Cancino Thank you so much 🌻🤗


Daryl Dixon Thank you for your helps too 🌻🤗


Terminal_Phantom ,Jayakrishna🇮🇳 ,Rohit Rajendra Brahmankar Thanks everyone 🤗 But still I'm not clear 🙆‍♀️ Can you please tell me why it's removing even numbers only?🤔