Consider the following code:

Position[{1, 2, 3, 0}, Except[0]]

It returns me:

{{0}, {1}, {2}, {3}, {}}

Instead of what I would expect:

{{1}, {2}, {3}}

Why? In the documentation of Except it is written "Except[c] is a pattern object which represents any expression except one that matches c." Shouldn't it work as the way I expect?

  • 1
    $\begingroup$ Look at the options for the function. 0 is the head. I'll leave it to you. $\endgroup$
    – ciao
    Aug 8 at 16:45
  • $\begingroup$ @ciao I am not sure to understand what you mean. There is no option "0" in the function Except when I look at the documentation. Perhaps you meant something else? $\endgroup$
    – StarBucK
    Aug 8 at 16:47
  • 1
    $\begingroup$ Test: expr = {1, 2, 3, 0}; pos = Position[expr, Except[0]]; Extract[expr, pos] -- see if any of the extracted positions matches the pattern 0. Perhaps you want Position[{1, 2, 3, 0}, Except[0], 1, Heads -> False], if you want "members" of the list. $\endgroup$
    – Michael E2
    Aug 8 at 16:48
  • $\begingroup$ @JasonB to some extent but not entirely. Thanks for the link. I actually opened another thread as I think my confusion is related to a more general concept mathematica.stackexchange.com/questions/288566/…. Thanks for the other comments they were helpful to fix the issue (but I still don't understand the "why"). $\endgroup$
    – StarBucK
    Aug 8 at 17:01


Browse other questions tagged or ask your own question.