Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Extract add extra {} to the result and what is best way to remove them

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?

like image 747
Robert H Avatar asked Dec 17 '22 11:12

Robert H


2 Answers

You probably realise this, but Position and Extract return lists because the requested values may be found in more than one position. So in general, removing the outer brackets doesn't make sense.

If you are sure the result is a singleton list, using Flatten would destroy information if the element is itself a list. For example,

Position[{{1}},1]

gives a list whose sole element is a list. So in this case, using Extract would make more sense.

Even so, there are many situations where Mathematica treats {x} very differently to x, as in

Position[1,1]
Position[{1},1]

which have very different results. So whether you can remove the outer braces from a one-member list depends on what you plan to do with it.

like image 71
James Avatar answered May 23 '23 05:05

James


If I understood your question correctly, you are asking why

lis = {{1}, {2}};
pos = Position[lis, {1}];
result = Extract[lis, pos]

returns

{{1}}

rather than

{1}

The answer is, I think, simple: Position[lis,{1}] gives the position at which {1}, not 1 appears in lis; when you then go and look at that position using Extract, you do indeed get {1} which is then wrapped in a list (which is exactly what happened in the first case, when you looked for 1 and obtained {1} as a result; just replace 1 by {1}, because that is now what you are asking for.

To see this more clearly, try

lis = {f[1], f[2]};
pos = Position[lis, f[1]];
result = Extract[lis, pos]

which gives

{f[1]}

The point here is that List in {1} (which is the same as List[1] if you check look at the FullForm) before was just a head, like f here. Should mathematica have remove f here? If not, then why should it have removed the innermost List earlier?

And finally, if you really want to remove the inner {} in your second example, try

lis = {{1}, {2, {1}}};
pos = Position[lis, {1}];
result = Extract[lis, pos, #[[1]] &]

giving

{1, 1}

EDIT: I am becoming puzzled with some of the answers here. If I do

lis = {{1}, {2, {1, 2, {1}}}};
pos = Position[lis, 1];
result = Extract[lis, pos]

then I get

{1, 1, 1}

in result. I only get the extra brackets when I actually obtain the positions of {1} in pos instead of the positions of 1 (and then when I look at those positions, I find {1}). Or am I missing something in your question?

like image 42
acl Avatar answered May 23 '23 04:05

acl