Command Position
always comes with two extra curly brackets {{}} when it's just one index for the location. I use [[1,1]] in front of my position commands. Is there a better way?
1 Answer
No, to my knowledge there isn't a better way, but there is a good reason for this behaviour.
First, the element might appear more than once, so Position
must return a list:
Position[{1, 2, 1, 3}, 1]
(* ==> {{1}, {3}} *)
Second, you might be searching a more complicated compound expression than a list, e.g. a matrix, so each list element might need to hold more than one index:
Position[{{1, 2, 1, 3}, {4, 5, 1, 6}}, 1]
(* ==> {{1, 1}, {1, 3}, {2, 3}} *)
Position[{1, {2, 1}}, 1]
(* ==> {{1}, {2, 2}} *)
Each index set that Position
returns can be directly used in the Extract
function, which is sometimes more convenient to use with Positions
's return value than Part
.
You might think that it would be better if Position
returned a single number as a special case when there's a single match only in a one-dimensional list, such as
Position[{1, 2, 3}, 1]
(* ==> {{1}} *)
If Position
did this, programs using it would not be easily generalizable to different inputs and the user would sometimes need to handle this special case.
If you only need the first occurrence of the pattern, you might consider using the fourth argument to your advantage (as Coolwater has pointed out in a comment above):
Position[list, patt, Infinity, 1]
immediately terminates when the first match is found so it is really useful for large redundant lists. If you are only interested in matches of the topmost level, use the third argument:
Position[list, patt, 1, 1]
This is equivalent to passing a vector as list
, that only has depth 1.
-
$\begingroup$ I gladly remove my answer, but would you mind if I edit in the fourth argument for
Position
as a shortcut? $\endgroup$ Commented Feb 26, 2014 at 20:11 -
$\begingroup$ @IstvánZachar Please do. We posted seconds apart! $\endgroup$– SzabolcsCommented Feb 26, 2014 at 20:13
[[1,1]]
not acceptable? The reasonPosition
does this is because in general, there can be more than one position where the element is matched and the position itself might be a list (e.g. in a multi-dimensional array). $\endgroup$Position[expr,val,Automatic,1]
if you didn't already do that... $\endgroup$First@First@Position
cant say better, just a matter of taste $\endgroup$