General
Part retains the head of an original expression, when given a list of positions to extract, rather than a single position. The same with Span, which is conceptually the same as a list of positions.
Part[h[1, 2, 3, 4, 5], 1]
Part[h[1, 2, 3, 4, 5], {3, 4, 5}]
Part[h[1, 2, 3, 4, 5], 3 ;; 5]
(*
1
h[3, 4, 5]
h[3, 4, 5]
*)
It makes sense. If you want to extract a sequence of parts, you want to group them into some container. Grouping them into original head rather than a List means that you create an "object" similar to the original one, but with fewer parts. This is much more useful behavior in a large number of cases, than if the parts would have been grouped always in a List. And if you want the latter behavior, it is always easy to get by Apply - ing List to the result.
Example: evaluation control
Here is just one (a bit artificial) example, where this is rather useful:
Part[Hold[1^2, 2^2, 3^2], 2]
(* 4 *)
but
Part[Hold[1^2, 2^2, 3^2], {2}]
(* Hold[2^2] *)
so in this case, this semantics of Part allowed us to prevent evaluation leak, if we wanted to.
The case of Associations
One case which stands out is that of Associations. We can start by noting a curious fact that Associations are atomic, and yet we can extract parts from them. In any case, the principle that the head of the expression is retained when several parts are extracted in a List, holds here too. Together with the head, the keys are also retained:
<|1 -> 2, 3 -> 4, 5 -> 6|>[[2]]
(* 4 *)
but
<|1 -> 2, 3 -> 4, 5 -> 6|>[[{2}]]
(* <|3 -> 4|> *)
and
<|1 -> 2, 3 -> 4, 5 -> 6|>[[{2, 3}]]
(* <|3 -> 4, 5 -> 6|> *)
This is in fact a very useful behavior of Associations, in a number of situations.
Partretains the head of an original expression, when given a list of positions to extract, rather than a single position. The same withSpan, which is conceptually the same as a list of positions. – Leonid Shifrin Nov 11 '15 at 18:32ListheadsPartwould give expression with the same head (=List). – mmal Nov 11 '15 at 18:39g[[1]]vsg[[{1}]]? – mmal Nov 11 '15 at 18:40Extract[g, {{2}, {3}}]gives{b,c}– eldo Nov 11 '15 at 18:47g[[1]] === Part[g, 1]is a single element extraction, so the element itself is returned.g[[{1}]] === Part[g,{1}]is an extraction of a list of elements, which happen to be just one element. In that case, you effectively getHead[g][sequence-of-extracted-elements]. WithAssociations, this is similar (although not quite the same) - there you get back anAssociationwith extracted key-value pairs, if you useassoc[[{parts}]]. – Leonid Shifrin Nov 11 '15 at 19:00{Part[g, 2], Part[f[a, b, c], 3]}, thanks! – mmal Nov 11 '15 at 19:03Head[g]instead ofList(pure curiosity)? – mmal Nov 11 '15 at 19:09Listmeans that you create an "object" similar to the original one, but with fewer parts. This is much more useful behavior in a large number of cases, than if the parts would have been grouped always in aList. And if you want the latter behavior, it is always easy to get byApply-ingListto the result. – Leonid Shifrin Nov 11 '15 at 19:48