[Numpy-discussion] Constructing array from generator expression/iterator

Travis Oliphant oliphant at ee.byu.edu
Wed Feb 8 14:59:03 CST 2006


Stephan Tolksdorf wrote:

> Hi
>
> I'm new to Numpy and just stumbled over the following problem in Numpy 
> 0.9.4:
>
> array(x**2 for x in range(10))
>
> does not return what one (me) would suspect, i.e.
>
> array([x**2 for x in range(10)])
>
> Is this expected behavior?


The array constructor does not current "understand" generators objects.  
It only understands sequence objects.   It could be made to work but is 
based on code written long before there were generators.  

So, instead you get a 0-d Object-array containing the generator.

Just use list comprehensions instead.

-Travis






More information about the Numpy-discussion mailing list