How to explain the strange behaviour about Function Nothing
Taking a look at ?a
shows what's going on:
?a
(* Global`a *)
(* a={Nothing,2,3,4,5,6,7,8,9,10} *)
It seems that Part
([[...]]
) does not apply the effect of Nothing
after the replacement has been done, leaving you with a list that is still 10 elements long. So the second a[[1]]=...
simply replaces the Nothing
in the first element with Nothing
again.
You case use Delete
to do the deletion properly:
{a = Range[10], a[[1]], a = Delete[a, 1], a}
(* {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 1, {2, 3, 4, 5, 6, 7, 8, 9,
10}, {2, 3, 4, 5, 6, 7, 8, 9, 10}} *)
{a[[1]], a = Delete[a, 1], a}
(* {2, {3, 4, 5, 6, 7, 8, 9, 10}, {3, 4, 5, 6, 7, 8, 9, 10}} *)
Part
assignment performs in-place modification of an expression without evaluation of the result. At the same time, on the Documentation page for Nothing
we read:
Nothing
is removed as part of the standard evaluation process.
So after evaluation of a[[1]] = Nothing
you still have a List
of length 10
with first element being Nothing
. You can replace Nothing
with anything else in the same way again:
a[[1]] = Nothing;
Definition[a]
a[[1]] = Missing[];
Definition[a]
a={Nothing,2,3,4,5,6,7,8,9,10} a={Missing[],2,3,4,5,6,7,8,9,10}
You can remove Nothing
by evaluating the expression:
a[[1]] = Nothing;
Definition[a]
a = a;
Definition[a]
a={Nothing,2,3,4,5,6,7,8,9,10} a={2,3,4,5,6,7,8,9,10}
Instead of Nothing
you can use such functions as Delete
, Drop
, Take
or ReplacePart
for the same purpose.