# Why does Position by default return a list of lists of positions instead of a simple list of positions?

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.

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

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.

• 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. – Mr.Wizard Sep 24 '17 at 19:44
• @Mr.Wizard It's really late here. I hope I won't forget to do it tomorrow. – Szabolcs Sep 24 '17 at 22:08
• 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. – Mr.Wizard Sep 25 '17 at 6:24