When I type the following
lis = {1, 2};
pos = Position[lis, 1];
result = Extract[lis, pos]
the result is always a list.
{1}
another example
lis = {{1}, {2}};
pos = Position[lis, {1}];
result = Extract[lis, pos]
{{1}}
Mathematica always adds an extra {} in the result. What would be the best way to remove this extra {}, other than applying Flatten[result,1] each time? And is there a case where removing these extra {} can cause a problem?
If I understood your question correctly, you are asking why
returns
rather than
The answer is, I think, simple:
Position[lis,{1}]gives the position at which{1}, not1appears inlis; when you then go and look at that position usingExtract, you do indeed get{1}which is then wrapped in a list (which is exactly what happened in the first case, when you looked for1and obtained{1}as a result; just replace1by{1}, because that is now what you are asking for.To see this more clearly, try
which gives
The point here is that
Listin{1}(which is the same asList[1]if you check look at theFullForm) before was just a head, likefhere. Should mathematica have removefhere? If not, then why should it have removed the innermostListearlier?And finally, if you really want to remove the inner
{}in your second example, trygiving
EDIT: I am becoming puzzled with some of the answers here. If I do
then I get
in
result. I only get the extra brackets when I actually obtain the positions of{1}inposinstead of the positions of1(and then when I look at those positions, I find{1}). Or am I missing something in your question?