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

Came across this late; however, just to add a non indexed approach as previously mentioned.

Tuple unpacking can be easily be applied to acquire the first and last elements. Note: The below code is utilizing the special asterisk '*' syntax which returns a list of the middle section, having a and c storing the first and last values.

Ex.

A= (3,4,4,4,4,4,4,3)
a, *b, c = A
print((a, c))

Output (3, 3)


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.)


s = (3,4,4,4,4,4,4,3)
result = s[0], s[-1]