18
$\begingroup$

There are many ways to extract different parts of lists in Mathematica. For example, the first part of a list v can be accessed either as v[[1]] or First[v] or Take[v,1]. Likewise the last element can be accessed as v[[-1]], Last[v] or Take[v,-1]. Similarly v[[2;;]] is equivalent to Rest[v] and to Drop[v,1], and v[[;;-2]] is equivalent to Most[v] and Drop[v,-1].

The more specialized functions First, Last, Rest and Most are marginally more efficient. On the other hand, it makes sense to use the more general Part ([[ ]]) or Take/Drop if it is part of a calculation that also requires parts to be accessed that do not have a specialist function.

Beyond these considerations, are there any reasons to prefer Part over Take/Drop or the more specialized functions, other than personal preferences over coding style?

$\endgroup$
0

2 Answers 2

21
$\begingroup$

You cannot make assignments to First, Last, Rest, or Most the way you can with Part. Therefore, there is greater consistency in using Part for all operations. See this answer for an example of and argument for this consistency.

Also, you must change functions if you need to update your code to index a different element or change an element to a Span. By using Part from the beginning you have the greatest flexibility without changing functions. This can make comparing different code revisions easier. It can also make a user defined function more flexible, e.g.: f[x_, part__ : 1] := foo @ x[[part]]

Less importantly x[[1]] is more terse than First@x, especially in the FrontEnd using 〚 〛, and I am a fan of terse coding.

$\endgroup$
18
  • $\begingroup$ +1, for discussing the maintenance cost. $\endgroup$
    – rcollyer
    Apr 9, 2012 at 0:29
  • 6
    $\begingroup$ In a sea of brackets, x[[1]] is less comprehensible than First@x and as a result probably less maintainable, albeit one character longer :) But whilst developing x[[1]] is easier to modify. Conclusion, develop with Part and deploy with First. $\endgroup$ Apr 9, 2012 at 8:07
  • 1
    $\begingroup$ Am the only one with Second, Third, Fourth, Fifth and Sixth defined too? :) $\endgroup$ Apr 9, 2012 at 8:10
  • 2
    $\begingroup$ @image_doctor I find that using \[LeftDoubleBracket] and \[RightDoubleBracket] greatly helps readability compared to [[ and ]] in the frontend $\endgroup$
    – acl
    Apr 9, 2012 at 9:25
  • 7
    $\begingroup$ First/Last are so elitist. Why not FirstAmongEquals and LastButNotLeast? $\endgroup$
    – Jens
    Apr 9, 2012 at 15:20
8
$\begingroup$

Putting aside any minor performance differences, the specialized functions look cleaner and clear in intent in cases where you know for sure that you will not be needing any arbitrary part. Some examples that come to mind are

1: expression//Timing//First            (* Get only the timing *)
2: (... Sow[x]...)//Reap//Last          (* Get only sowed values *)
3: Most@#/Last@#&@CoefficientList[poly] (* normalize by coefficient of highest degree term *)
4: Rest@#/First@#&@Eigenvalues[matrix]  (* normalize by largest eigenvalue *)
5: Most@#/Rest@#&@list                  (* divide each element by the next *)

I'm not very into golfing, but I do like minimizing my keystrokes that are spent doing nothing (and avoid the mouse). If I'm at the end of a long expression and I need to take the first element, I'd simply use Part to extract it rather than move the cursor all the way back to insert a First@. In other words:

something + (insert very long expression here)[[1]]

instead of

something + First@(insert very long expression here)

If I change my mind and decide I need a different element, I have the flexibility to do so and also include all elements with [[;;]]. In such cases, you can't simply stick in a //First at the end, because of the very low precedence of Postfix — the expression returned might not be what you intended to get.

$\endgroup$
2
  • $\begingroup$ In the case of something + (insert very long expression here) (already bracketed as shown) you can avoid the mouse or excessive left-cursor-key by pressing: Ctrl + . + . and then left-cursor a single time. Four keystrokes total. $\endgroup$
    – Mr.Wizard
    Apr 9, 2012 at 14:11
  • $\begingroup$ @Mr.Wizard Not a very big fan of Ctrl+. — never been able to wrap my head around what it's going to select next. Of course, that might be just due to unconventional learning and a reluctance to use it, but I might get around to taming it like I did for Infix :) $\endgroup$
    – rm -rf
    Apr 9, 2012 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.