1
$\begingroup$

This question already has an answer here:

For me, the natural meaning of the function Part is "get the sub-expression at appointing position." It is quite unlike Take, Take always keeps the head in the process. For example, by using Take:

Take[f[a, b, c[i], d[j, k], e, f], {3, 4}, 1]
(* Take the third to fourth position at level 1; and first position at level 2 *)
(* Output: f[c[i], d[j]] *)

When using Part:

Part[f[a, b, c[i], d[j, k], e, f], 4, 2]
(*Take the expression directly at the position 4, 2*)
(* Output: k *)

Until now, the mechanism of both functions is quite natural to me. However, the next result leaves me feeling confused:

f[a, b, c[i], d[j, k], e, f][[4]]
f[a, b, c[i],d[j, k], e, f][[4;;5]]

The output is d[j, k] versus f[d[j, k], e]. I don't know why I get the latter result -- where the extracted parts are wrapped with f. I was expecting {d[j, k], e}.

Is this design reasonable or useful in most cases? What is its intended use?

$\endgroup$

marked as duplicate by m_goldberg, Coolwater, LCarvalho, Kuba list-manipulation Feb 21 '18 at 22:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ It is documented in ref/ Part details section. And it can be useful Hold[Print[1], Print[2]][[{1}]] $\endgroup$ – Kuba Feb 21 '18 at 14:09
  • $\begingroup$ @Kuba I just found that the usage of Part[expr,{n}] seems undocumented. $\endgroup$ – Eric Feb 21 '18 at 14:18
  • 1
    $\begingroup$ How is that undocumented? Isn't it the 4th usage? And see also the 4th bullet point in Details section. $\endgroup$ – Kuba Feb 21 '18 at 14:19
  • $\begingroup$ Ya, but the document said "gives a "list" of the parts $i_1,~i_2,~\cdots$ of expr." According to it, the result should be surrounded by the {}(namely, a list) right? $\endgroup$ – Eric Feb 21 '18 at 14:24
  • 1
    $\begingroup$ This and this QAs seem relevant, if not duplicates. $\endgroup$ – Leonid Shifrin Feb 21 '18 at 19:08