The question is, what do you want to do with the output. The output of Position
is in a form so that it can directly be used with Extract
list = {a, b, a, a, b, c, b};
pos = Position[list, b];
Extract[list, pos]
(* {b, b, b} *)
For this simple example, it is a bit useless because we already know, that on all positions pos
we have a b
in list
. Unfortunately, your simple example covers another thing, which is that you really need the nested structure {{2}, {5}, {7}}
in non-trivial cases.
If you really just want the values, than I suggest (as the others too) to use Flatten
Flatten[pos]
(* {2, 5, 7} *)
When your input is really just a one-dimensional structure or list, the above can be used with Part
to extract the values of list
Part[list, Flatten[pos]]
(* or a bit shorter *)
list[[Flatten[pos]]]
In most cases you cannot simply flatten the output and to give you a small non-trivial example let's assume we have a somewhat moderate complex expression
expr = 20 x^3 Cos[x + x^5] - Sin[x + x^5] - 10 x^4 Sin[x + x^5] - 25 x^8 Sin[x + x^5]
Now you want to use Position
to find all positions where x
appears with an exponent
pos = Position[expr, x^_]
(* {{1, 2}, {1, 3, 1, 2}, {2, 2, 1, 2}, {3, 2}, {3, 3, 1, 2}, {4, 2}, {4, 3, 1, 2}} *)
What you see is, that the structure is nested and this has a reason: To find the first x^_
which is at position {1,2}
you have to select the first part which is
20 x^3 Cos[x + x^5]
and there the second one. Even more nested it gets, when the search pattern in located inside one of the Sin
's. If you flatten this output of Position
you get nothing useful but feeding the output to Extract
gives you everything you wanted
Extract[expr, pos]
(* {x^3, x^5, x^5, x^4, x^5, x^8, x^5} *)
Flatten
. $\endgroup$Out[2]=2,5,7
is not mathematica syntax. Do you meanOut[2]={2,5,7}
? $\endgroup$Out[2]={2,5,7}
is possible and trivial. To do it useFlatten
, as suggested by @b.gatessucks. The question "How to manipulate it" as so many possible interpretations that I can't answer. Otherwise I don't understand : "so it come to be ..." $\endgroup$