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

+33

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

+29

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

+18

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 https://code.sololearn.com/cSenMrKUS4OC/?ref=app

+15

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

+14

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

+12

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

+11

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

+9

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

+8

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

+8

Sanjay Kamath Thank you 😊

+7

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

+7

vamsi Thank you too 😊

+7

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

+6

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

+6

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

+6

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

+6

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 🙂

+6

Sanjay Kamath Yes, it helped me 😊

+6

Julio Lopez Cancino Thank you so much 🌻🤗

+6

Daryl Dixon Thank you for your helps too 🌻🤗

+5

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?🤔