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]] = Nothing
changes the first element ofx
to beNothing
. It does not re-evaluate the entire contents ofx
and then re-assign the result tox
. $\endgroup$Nothing
is not a function. It is a symbol.Sequence
is very different: you must useSequence[]
. It has to be used as a function. If you are usingNothing
and notNothing[]
in your code, then please do not keep referring toNothing[]
—they are not the same thing. $\endgroup$