If a
is your original iterable, you could do something along the following lines:
np.all(np.array(a, dtype=np.int8) == a)
Quite simply, this compares the resulting ndarray
to the original values, and tells you whether the conversion to ndarray
has been lossless.
This will also catch things like using a floating-point type that's too narrow to represent some of the values exactly:
>>> a = [0, 0, 0, 0, 0, 0.123456789]
>>> np.all(np.array(a, dtype=np.float32) == a)
False
>>> np.all(np.array(a, dtype=np.float64) == a)
True
Edit: One caveat when using the above code with floating-point numbers is that NaNs always compare unequal. If required, it is trivial to extend the code to handle that case too.