Signup/Sign In

NumPy fromiter() function

In this tutorial, we will cover numpy.fromiter() function of Numpy library.

The numpy.fromiter() function is used to create an ndarray by using a python iterable object. This method mainly returns a one-dimensional ndarray object.

Syntax of numpy.fromiter():

Below we have the required syntax to use this function:

numpy.fromiter(iterable, dtype, count)  


Let us discuss the parameters of the above function:

  1. iterable
    This parameter is used to represents an iterable object.

  2. dtype
    This parameter is used to represent the data type of the resultant array items.

  3. count:
    This parameter is used to represent the number of items to read from the buffer in the array.

Note: It is important to specify a count parameter in order to improve performance of this function. Because the count parameter allows the fromiter() function to pre-allocate the output array rather than resizing it on demand.

Returned Value:

This function will return the array created using the iterable object.

Let us now discuss some examples using fromiter() function.

Basic Example:

Below we have the code snippet of the example using this function:

import numpy as np  

a = [0,2,4,9,10,8]  
it = iter(a)  
x = np.fromiter(it, dtype = float)  

print("The output array is :")

print("The type of output array is:")

The output array is :
[ 0. 2. 4. 9. 10. 8.]
The type of output array is:
<class 'numpy.ndarray'>


In this tutorial we covered numpy.fromiter() function in the Numpy library. We also covered its syntax, parameters as well as the value returned by this function along with code example.

About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.