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
+ 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
+ 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
+ 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
+ 6
Like this
List 01234567
Index 0
List 1234567
Index 1
List 134567
Index 2
List 13567
Index 3
List 1357
Output 1357 🤠
+ 5
The index variable is incrementing after every iteration. That is how the for loop is woking internally to iterate through the list
+ 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.
+ 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
+ 4
Risha 🌨 hope it helps 🤔
+ 3
16
+ 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.
+ 3
Don't understand the code. Try to understand the output
https://code.sololearn.com/cQVGuQU8z0UP/?ref=app
+ 3
You must use iterator while looping
+ 3
So that it will be empty and sum will be zero
+ 2
Here, iterating means going through the elements of the list
+ 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.
+ 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.
+ 1
0
+ 1
How can it be 16 Risha
+ 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.