2
$\begingroup$
Position[{1,2,3,4,2,3,4,5},2]
(* Output: {{2},{5}} *)

Why does Position work this way? I understand this form might be appropriate for, say, different levelspecs or perhaps other options, but I feel this behavior being default, especially for such a simple example as above, would be unexpected by newcomers.

I'm currently doing [[All,1]] to get the list I want, but I'm still curious.

$\endgroup$
  • 4
    $\begingroup$ It keeps things completely consistent regardless of dimensions of searched target and/or level spec. $\endgroup$ – ciao May 16 '16 at 5:28
  • $\begingroup$ In many functions n will give you first n elements, and {n} will give you the nth element. $\endgroup$ – happy fish May 16 '16 at 5:30
  • 1
    $\begingroup$ Flatten will work too. $\endgroup$ – Öskå May 16 '16 at 8:46
  • 4
    $\begingroup$ Because Position[] is actually intended for use with Extract[] instead of Part[]. $\endgroup$ – J. M. will be back soon May 16 '16 at 9:29
  • $\begingroup$ Related: (84424) $\endgroup$ – Mr.Wizard Aug 7 '17 at 12:34
5
$\begingroup$

It returns standard position specifications that can be used directly with other functions such as Extract.

Position specifications work with expressions deeper than a list (e.g. matrix, ragged list, or any arbitrary expression). In this case they must contain more than one index. Thus they take the form of a list.

Standard position specifications are used at least with Extract, MapAt, ReplacePart and Position. In the general case these functions handle lists of position specifications. Since position specifications themselves are lists too, it is important to be able to distinguish between a single position specification or a list of them. Thus it is absolutely necessary that even one-index one are a (single element) list. Otherwise {1,2} would be ambiguous: it could represent a single position specification with two indices, or a list of two one-index position specifications.

Having a consistent standard form for position specifications is thus useful and makes it easy to write general code that works fine in edge cases too.

Of course you are right that having {k} instead of k for the most common use case seems inconvenient. I believe that is one reason why we also have Part in addition to extract. Part uses a different syntax for part specifications and is more convenient for standard array indexing.

In general, you would use Part when indexing directly, and you would use Extract with the output of Position.

$\endgroup$
  • $\begingroup$ While searching for duplicates for (156438) a more recent question I found both this one, and (42988). Since you alone answered both questions I wonder if these could be combined to form a canonical Q&A. As you can see from the links on 156438 this question keeps popping up in one form or another. $\endgroup$ – Mr.Wizard Sep 24 '17 at 19:44
  • $\begingroup$ @Mr.Wizard It's really late here. I hope I won't forget to do it tomorrow. $\endgroup$ – Szabolcs Sep 24 '17 at 22:08
  • $\begingroup$ By all means don't feel any pressure to do so; it was merely a suggestion that you might apply if you have a boring day. $\endgroup$ – Mr.Wizard Sep 25 '17 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.