How to get first AND last element of tuple at the same time

indexing, python, slice, tuples

Solution

I don't know what's wrong about

(s[0], s[-1])

A different option is to use `operator.itemgetter()`:

from operator import itemgetter
itemgetter(0, -1)(s)

I don't think this is any better, though. (It might be slightly faster if you don't count the time needed to instantiate the `itemgetter` instance, which can be reused if this operation is needed often.)

Problem

I need to get the first and last dimension of an numpy.ndarray of arbitrary size. If I have `shape(A) = (3,4,4,4,4,4,4,3)` my first Idea would be to do `result = shape(A)[0,-1]` but that doesn't seem to work with tuples, why not ?? Is there a neater way of doing this than ``` s=shape(A) result=(s[0], s[-1]) ``` Thanks for any help

Original source