Python Challenge Question ๐Ÿ’ฌ | SoloLearn: Learn to code for FREE!

+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 โญ

56 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

+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

Ahmed Kamal read the answers one by one. Your doubt will be clear ๐Ÿ™‚

+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 ๐Ÿ˜Š