NumPy Tutorial NumPy Statistics NumPy References

NumPy - fromiter() function



The NumPy fromiter() function is used to create a new 1-dimensional array from an iterable object. The syntax for using this function is given below:

Syntax

numpy.fromiter(iterable, dtype, count=-1)

Parameters

iterable Required. Specify an iterable object providing data for the array.
dtype Required. Specify the desired data type of returned array.
count Optional. Specify the number of items to read from iterable. The default is -1, which means all data is read.

Return Value

Returns an array created from iterable object.

Example:

In the example below, the fromiter() function is used to create a numpy array from an iterable object.

import numpy as np

it = (x*x for x in range(5))

#creating numpy array from an iterable
Arr = np.fromiter(it, dtype=float)
print("Arr is:", Arr)

The output of the above code will be:

Arr is: [ 0.  1.  4.  9. 16.]

❮ NumPy - Functions