NumPy

Previous topic

numpy.fromfunction

Next topic

numpy.fromstring

numpy.fromiter

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

Create a new 1-dimensional array from an iterable object.

Parameters
iterableiterable object

An iterable object providing data for the array.

dtypedata-type

The data-type of the returned array.

countint, optional

The number of items to read from iterable. The default is -1, which means all data is read.

Returns
outndarray

The output array.

Notes

Specify count to improve performance. It allows fromiter to pre-allocate the output array, instead of resizing it on demand.

Examples

>>> iterable = (x*x for x in range(5))
>>> np.fromiter(iterable, float)
array([  0.,   1.,   4.,   9.,  16.])