[Numpy-discussion] Should array iterate over a set?

Robert Kern robert.kern at gmail.com
Sat Nov 17 21:42:23 EST 2007

Michael McNeil Forbes wrote:
> My expectation was that array would iterate over a set.  This is  
> incorrect:
>  >>> array(set([1,2,3]))
> array(set([1, 2, 3]), dtype=object)
> Is this the intended behaviour?  A trivial work-around that does what  
> I need is
>  >>> array(list(set([1,2,3])))
> array([1, 2, 3])
> but I was wondering if this was by design or just a forgotten  
> corner.  (Maybe a vestige of the tuple special case for record arrays?)

We can recognize most sequences (i.e. for all i in range(len(x)), x[i] responds
correctly), but we cannot easily deal with arbitrary iterables which are not
sequences in the array() function. There are a lot of special cases and
heuristics going on in array() as it is. Instead, we have a fromiter() function
which will take an iterable and construct an array from it. It is limited to 1D
arrays, but this is by far the most common use for constructing an array from an

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list