I would like to use Nothing[] to make some elements of a list invisible. (The intended use is to remove a few unwanted points from a ListPlot.) But sometimes it seems that the operation fails:
x = {a, b, c, d, e} (* Create a list *)
(*{a, b, c, d, e}*)
x[[3]] = Nothing (* Remove an element *)
(*Nothing*)
x
(*{a, b, d, e} (* Third element successfully removed. *)*)
x[[3]]
(*d (* The third element is now 'd' instead of 'c' *)*)
x[[1]] = Nothing (* Remove the first element *)
(*Nothing*)
x
(*{b, d, e} (* The first element removed successfully. *)*)
x[[3]] = Nothing (* Remove the third element *)
(*Nothing*)
x
(*{b, d, e} (* List is unchanged! *)*)
x[[3]]
(*e*)
Of course, assigning Nothing[] does not actually 'delete' anything.
Definition[x]
(*x = {Nothing, b, Nothing, d, e}*)
The documentation says "Nothing[] is removed during the standard evaluation process." This then makes me believe that assigning Nothing[] does not involve the standard evaluation process.
This is rather annoying, because now the indexes used by standard evaluation and the Nothing[] assignment do not match.
Position[x, d]
(*{{2}} (* Yes, the element 'd' is in the list *)*)
x[[Position[x, d] // First]] = Nothing[] (* Try to remove element 'd' *)
(*Nothing*)
x
(*{d, e} (* The element 'd' was not removed! *)*)
x[[1]] = z; x
(*{z, d, e} (* This is just weird *)*)
I guess I'm posting this more as a warning than as a question. If there are suggestions on how to do it differently, or if I'd missed something in the documentation that explains this behaviour I'd be most interested.
My next step would be to collect the indices of the elements I want to disappear and assign Nothing[] to them in a singe step, rather than trying to remove them piecemeal.
x[[1]] = Nothingchanges the first element ofxto beNothing. It does not re-evaluate the entire contents ofxand then re-assign the result tox. – Szabolcs Mar 04 '20 at 16:33Nothing[]. In fact I find that usage strange.Nothingis the usual way.Nothing[x]something one would normally create withApplyor byReplaceing a head (Missing->Nothing) – Szabolcs Mar 04 '20 at 19:47Nothing[]instead ofNothingto make it clear that I meant the Mathematica function and not the common word 'nothing'. It also matches the way `Sequence[]' is used for the same purpose. – Niel Malan Mar 05 '20 at 08:26Nothingis not a function. It is a symbol.Sequenceis very different: you must useSequence[]. It has to be used as a function. If you are usingNothingand notNothing[]in your code, then please do not keep referring toNothing[]—they are not the same thing. – Szabolcs Mar 05 '20 at 08:49