Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 30
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
29th Aug 2020, 4:09 PM
Rohit Rajendra Brahmankar
Rohit Rajendra Brahmankar - avatar
+ 21
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
29th Aug 2020, 4:18 PM
Terminal_Phantom
Terminal_Phantom - avatar
+ 15
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
29th Aug 2020, 4:36 PM
Jayakrishna 🇮🇳
+ 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
29th Aug 2020, 5:30 PM
Lothar
Lothar - avatar
+ 6
Like this List 01234567 Index 0 List 1234567 Index 1 List 134567 Index 2 List 13567 Index 3 List 1357 Output 1357 🤠
30th Aug 2020, 3:39 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 5
The index variable is incrementing after every iteration. That is how the for loop is woking internally to iterate through the list
29th Aug 2020, 4:36 PM
Terminal_Phantom
Terminal_Phantom - avatar
+ 5
To understand it visually, I added two print() statements, one before the .remove() to print the value of "i", and the other one after .remove() to get the actual list. <\> myList = [i for i in range(8)] for i in myList: print(i) myList.remove(i) print(myList) <\> #output 0 #the value of the first index was 0, [1, 2, 3, 4, 5, 6, 7] #0 was removed from the list 2 #value for the second index was 2 [1, 3, 4, 5, 6, 7] #2 was removed from the list 4 #value for the third index was 4 [1, 3, 5, 6, 7] #4 was removed from the list 6 #value for the fourth index was 6 [1, 3, 5, 7] #6 was removed from the list.
31st Aug 2020, 5:09 AM
Julio Lopez Cancino
Julio Lopez Cancino - avatar
+ 4
I think your doubt is why not 0..? If yes, then there i is index value. So removing At index 0, list is 1 2 3,4 5 6 7 Index at 1 removing, 1,3 4,5, 6, 7 Index at 2, removing 1,3, 5,6,7 Index at 3, removed, list is 1,3,5,7 No value at index 4. So sum is 16 Edit : Risha 🌨 Add your doubt in question clearly.. Otherwise it's seems like asking only output.. You can get that by playground.. Mods may don't read entire thread.. So they may delete as homework question.. If you require it further, add it some more clarity
29th Aug 2020, 4:13 PM
Jayakrishna 🇮🇳
+ 4
Risha 🌨 hope it helps 🤔
31st Aug 2020, 1:31 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 3
16
29th Aug 2020, 4:05 PM
Terminal_Phantom
Terminal_Phantom - avatar
+ 3
Internally you can think like the for loop has a index count, and it increments after every iteration. It's hard to understand what I am explaining. Because you have to keep track of two things at a time.
29th Aug 2020, 4:22 PM
Terminal_Phantom
Terminal_Phantom - avatar
+ 3
Don't understand the code. Try to understand the output https://code.sololearn.com/cQVGuQU8z0UP/?ref=app
29th Aug 2020, 4:34 PM
Terminal_Phantom
Terminal_Phantom - avatar
+ 3
You must use iterator while looping
31st Aug 2020, 5:36 AM
Shivam Rawal
+ 3
So that it will be empty and sum will be zero
31st Aug 2020, 5:36 AM
Shivam Rawal
+ 2
Here, iterating means going through the elements of the list
29th Aug 2020, 4:24 PM
Terminal_Phantom
Terminal_Phantom - avatar
+ 2
myList before remove= [0, 1, 2, 3, 4, 5, 6, 7] myList after remove= [1, 2, 3, 4, 5, 6, 7] myList before remove= [1, 2, 3, 4, 5, 6, 7] myList after remove= [1, 3, 4, 5, 6, 7] myList before remove= [1, 3, 4, 5, 6, 7] myList after remove= [1, 3, 5, 6, 7] myList before remove= [1, 3, 5, 6, 7] myList after remove= [1, 3, 5, 7] 16 Risha 🌨 After going for index 0 it moves to the next index 1.
31st Aug 2020, 8:59 AM
Abhishek Kumar
Abhishek Kumar - avatar
+ 2
Interpreting the first line, the list myList will contain [0,1,2,3,4,5,6,7] Now, let's start iterating from the first element. 0 gets removed. The list now becomes [1,2,3,4,5,6,7], but in the loop we have moved on to the index 1 and which in the current list is 2. Hence, this time 2 gets removed and not 1. The same pattern repeats removing every alternate element. The list after the loop remains [1,3,5,7] Summing it up gives us 16 as the output.
31st Aug 2020, 11:42 AM
विशेष
विशेष - avatar
+ 1
0
31st Aug 2020, 6:53 AM
Syfar 03
Syfar 03 - avatar
+ 1
How can it be 16 Risha
31st Aug 2020, 12:16 PM
Ahmed Kamal
Ahmed Kamal - avatar
+ 1
Sum of Consecutive Numbers No one likes homework, but your math teacher has given you an assignment to find the sum of the first N numbers. Let’s save some time by creating a program to do the calculation for you! Take a number N as input and output the sum of all numbers from 1 to N (including N). Sample Input 100 Sample Output 5050 Explanation: The sum of all numbers from 1 to 100 is equal to 5050.
24th Feb 2021, 12:03 AM
Muhammed gomaa elsafy
Muhammed gomaa elsafy - avatar