Why extract adds extra {} to the result and what is the best way to remove them

When i type next

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 extra {} to the result. What would be the best way to remove this extra {}, except using Flatten [result, 1] every time? And is there a case where removing these extra {} can cause a problem?

+4
source share
3 answers

If I understand your question correctly, you ask why

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

returns

 {{1}} 

but not

 {1} 

The answer, I think, is simple: Position[lis,{1}] gives the position in which {1} , and not 1 appears in lis ; when you go and look at this position using Extract , you really get {1} , which is then wrapped in a list (this is exactly what happened in the first case, when you searched for 1 and got the result, just replace 1 with {1} because this is what you are asking for.

To make it clearer, 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} (the same as List[1] , if you look at FullForm ), used to be just a head, for example f here. Should mathematicians remove f here? If not, why would he remove the innermost List before?

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

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

gives

 {1, 1} 

EDIT: I am puzzled by 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 get extra brackets when I actually take positions {1} in pos instead of positions 1 (and then when I look at these positions, I find {1} ). Or am I missing something in your question?

+4
source

You probably understand this, but Position and Extract return lists, because the requested values ​​can be found in several positions. Therefore, generally speaking, removing external brackets does not make sense.

If you are sure that the result is a single list, using Flatten will destroy information if the item itself is a list. For instance,

 Position[{{1}},1] 

gives a list whose only element is a list. Therefore, in this case, using Extract makes sense.

However, there are many situations where Mathematica treats {x} very differently with x, as in

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

which have very different results. Thus, whether it is possible to remove external curly braces from a list from one member depends on what you plan to do with it.

+6
source

The short answer . You should probably use First@Position [...]

Long answer: Let's divide the question into 2 parts:

Why do you have an extra {} result for Position ?

i.e. why:

 lis = {1, 2}; Position[lis, 1] 

returns {{1}} ?

This is in order to work sequentially with an n-dimensional list, which can have the requested values ​​in several positions. For instance:

 lis = {{1, 2, 3}, {1, 5, 6}, {1, 2, 1}}; Position[lis, 1] 

returns {{1, 1}, {2, 1}, {3, 1}, {3, 3}}

which is a list of coordinates, the result is found. So in your case:

 lis = {1, 2}; Position[lis, 1] 

return {{1}} , as in: we found your requested value once, at coordinate {1} .

Mathematica many times now assumes that there may be a list of solutions (for example, in Solve ), but the user knows that he expects only one. The appropriate code for this in your case would be First@Position [...] . this will return the first (and, presumably, only) element in the list of positions - So, if you are sure that the element you are looking for exists only once in the list and want to know where, use this method.

Why do you have an extra {} result for Extract ?

Extract can work in two different ways.

If I do Extract[{{a, b, c}, {d, e, f}, {g, e, h}}, {1, 2}] I get b , so extracting with a 1-dimensional list just selects and returns this item. Indeed, Extract[lis, {1, 2}] equals lis[[1, 2]]

If I do Extract[{{a, b, c}, {d, e, f}, {g, e, h}}, {{1, 2}, {3, 4}}] I get {b, h} , so extraction with a 2-dimensional list selects and returns a list of elements.

In your case (s), you do Extract[lis, {{1}}] , as in: give me a list containing only the lis[[1]] element. The result is always this element in the list, which is optional {}

+2
source

All Articles