7

Someone commented on one of my answers that the code no longer works. They said “I think a major point is how to retrieve those values (0:2 and 3: in this case). What you replace the elements 4 and 3? Your code no longer works and you need to change it...”: https://stackoverflow.com/a/43283924/3474956

I am not sure what they mean. The code still works with Python 3.7. I think my post answers the question. The desired entries in the list are removed when my code is run. Am I missing something? Do I need to change that answer?

3
  • 7
    Their point is that although s[0:2] + s[3:] works (as would s[:2] + s[3:]) you leave out how to determine the index (here 2) to skip, or deal with the possibility that the value might appear at more than one index. The question was how to delete by value, not index.
    – jonrsharpe
    Commented Jul 29, 2020 at 21:25
  • 5
    Leave them a comment in reply asking for clarification. See How do comment @replies work? to learn to use @x to notify one non-sole non-poster commenter x re a comment.
    – philipxy
    Commented Jul 29, 2020 at 23:05
  • In any case, perhaps state the version and platform you got the result on? E.g. "Tested with Python 3.1.19 on Windows.". Commented Jul 31, 2020 at 11:25

1 Answer 1

12

That comment has a couple of grammatical typos, which probably make it harder to understand. It's pretty clear the commenter intended to write the following:

What if you swap the elements 4 and 3? Your code no longer works and you need to change it.

In other words, even if your answer works in the specific case you show, it is not general and will not work if the value 3 is moved to a different position. The commenter is outlining the fact that you should focus on how to get those indexes first, and then explain how to slice the value away from the list.

Something like this:

>>> s = [5,4,3,2,1]
>>> i = s.index(3)
>>> s[:i] + s[i+1:]
[5, 4, 2, 1]

For the above reason, in its current state, yours isn't really a valuable answer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .