As others have written, list()
works because an array is an iterable. It is equivalent to [i for i in arr]
. To understand it you need to understand how iteration over an array works. In particular, list(arr)
is not the same as arr.tolist()
.
In [685]: arr=np.array('one two three four'.split())
In [686]: arr
Out[686]:
array(['one', 'two', 'three', 'four'],
dtype='<U5')
In [687]: ll=list(arr)
In [688]: ll
Out[688]: ['one', 'two', 'three', 'four']
In [689]: type(ll[0])
Out[689]: numpy.str_
In [690]: ll1=arr.tolist()
In [691]: ll1
Out[691]: ['one', 'two', 'three', 'four']
In [692]: type(ll1[0])
Out[692]: str
The print display of ll
and ll1
looks the same, but the type
of the elements is different, one is a str
, the other a str
wrapped in a numpy
class. That distinction has come up in a recent question about serializing an array.
The distinction becomes more obvious when arr
is 2d. Simple interation then produces the rows, not the elements:
In [693]: arr=np.reshape(arr,(2,2))
In [694]: arr
Out[694]:
array([['one', 'two'],
['three', 'four']],
dtype='<U5')
In [695]: list(arr)
Out[695]:
[array(['one', 'two'],
dtype='<U5'), array(['three', 'four'],
dtype='<U5')]
In [696]: arr.tolist()
Out[696]: [['one', 'two'], ['three', 'four']]
list(arr)
is now two arrays, while arr.tolist()
is a nested list.
Python Pandas: use native types
Why does json.dumps(list(np.arange(5))) fail while json.dumps(np.arange(5).tolist()) works