NumPy array creation: fromiter() function

fromiter() function

The fromiter() function is used to create a new 1-dimensional array from an iterable object.


numpy.fromiter(iterable, dtype, count=-1)
NumPy array: fromiter() function

Version: 1.15.0


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

Return value:

out : ndarray
The output array.

Example-1: NumPy.fromiter() method

>>> import numpy as np
>>> iterable = (a*a for a in range(6))
>>> np.fromiter(iterable, float)
array([  0.,   1.,   4.,   9.,  16.,  25.])

Pictorial Presentation:

NumPy array: fromiter() function

Python - NumPy Code Editor:

Previous: fromfunction()
Next: fromstring()