# Numpy.array with dtype works on list of tuples not on list of lists?

Robert Kern robert.kern at gmail.com
Sun Sep 18 22:31:58 CEST 2011

```On 9/18/11 10:55 AM, Alex van der Spek wrote:
> Why does this not work?
>
>>>> dat=[[1,2,3],[4,5,6]]
>>>> col=[('a','f4'),('b','f4'),('c','f4')]
>>>> arr=numpy.array(dat,dtype=col)
>
> Traceback (most recent call last):
> File "<pyshell#91>", line 1, in <module>
> arr=numpy.array(dat,dtype=col)
> TypeError: expected a readable buffer object
>>>>
>
> But this does:
>
>>>> dat=[(1,2,3),(4,5,6)]
>>>> arr=numpy.array(dat,dtype=col)
>>>> arr
> array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)], dtype=[('a', '<f4'), ('b', '<f4'),
> ('c', '<f4')])
>>>>
>
> The only difference that the object is a list of tuples now?

numpy questions are best asked on the numpy mailing list:

http://www.scipy.org/Mailing_Lists

To answer your question, though, numpy.array() needs to figure out a lot of
different things about the input data simultaneously, in particular its shape.
Structured arrays (i.e. with elements that have individual fields as above) pose
a new problem in that its individual elements are sequences themselves. In order
to help it decide whether it should recurse down into a sequence to find its
elements or decide that the sequence *is* an element in its own right, we
settled on the convention that tuples are to be considered elements and that
lists are sequences of elements.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma