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?
Comments
Post a Comment