+40

# 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

Ginny โญ

+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

+20

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

+14

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

+13

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

+8

Hardik Rajubhai Poriya & เคตเคฟเคถเฅเคท Thank you both ๐

+7

โคโขโ ๐๐ข๐ข๐๐จ ๐๐ก๐๐ฒ๐๐ฅ โโขโค I thought you would understand it, like others ๐

+7

Sanjay Kamath Thank you ๐

+7

vamsi Thank you too ๐

+7

+7

Daryl Dixon Thank you for your helps too ๐ป๐ค

+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

Jayakrishna๐ฎ๐ณ, Terminal_Phantom & Lothar Thanks again for taking your time I'm clear now ๐ป๐ค

+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 ๐ป๐ค

+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?๐ค

+5

The index variable is incrementing after every iteration. That is how the for loop is woking internally to iterate through the list

+5

Abhishek Kumar Thank you ๐