Delete_starting_evens() challenge

I am a bit confused why the code I have come up with is not working. Can someone explain? By my understanding, the temporary variable “number” should be 8 on the second iteration of the for loop, but it keeps coming out as 10, as you can see in the output.

The hint suggests using a while loop, but the reason they do it seems irrelevant to me so I want to try and complete this my own way.

Here is a link to the page: https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-code-challenges/articles/python-code-challenges-loops

Removing from the list you are looping over is problematic. Lets say we have the list with the indexes:

4   8   10 # values in the list
0   1   2  # indexes/position of elements/values in the list

when the value 4 (index 0) is removed, we get:

_   8   10
0   1   2

where the underscore is the absence of the value. But the list can’t exists in this state, so everything to the right of the removed item will shift:

8   10
0   1

the for loop uses indexes to keep track of the iteration. So the loop will go to the next index. Which is index 1, containing the value 10. The 8 is skipped due to the shift

3 Likes

Ah thanks so much. As I thought about it some more, I figured it was something like this, so the confirmation is greatly appreciated. Cheers.

1 Like

Tools like this:

Python Tutor - Visualize Python, Java, JavaScript, C, C++, Ruby code execution

Can be really helpful in such cases, which allow you to step through your code and inspect the state of you application

1 Like