Numpy function to get last element of 1-d array

84 views Asked by At

Given a numpy array a, is there any alternative to

a[-1]

to get the last element?

The idea is to have some aggregating numpy method as

np.last(a)

that could be passed to a function to operate on a numpy array:

import numpy as np


def operate_on_array(a: np.array, np_method_name: str):
     method = getattr(np, np_method_name)
     return method(a)

This would work for methods such as np.mean, np.sum but I have not been able to find if there is some numpy method name that would return the last or first element of the array.

1

There are 1 answers

1
mozway On

What about a lambda?

lambda x: x[-1]