I think we can do better if we use fromiter and supply the size as count:
np.fromiter ((u**2 for u in range(10)), dtype=float, count=10)
On Fri, Jun 23, 2023 at 10:39 AM Joao S. O. Bueno
wrote:
> If you use Python's own arrays and generator expressions instead of list
> comprehension (by just
If you use Python's own arrays and generator expressions instead of list
comprehension (by just dropping the `[ ]`s),
you will get each number converted to the target type in memory as soon as
it is calculated. (It will be a full
Python float/int instance during the calculation itself, though).