What is the difference between applying list()
on a numpy
array vs. calling tolist()
?
I was checking the types of both output
The major difference is that tolist
recursively converts all data to python standard library types.
For instance:
>>> arr = numpy.arange(2)
>>> [type(item) for item in list(arr)]
[numpy.int64, numpy.int64]
>>> [type(item) for item in arr.tolist()]
[builtins.int, builtins.int]
Aside from the functional differences tolist
will generally be quicker as it knows it has a numpy array and access to the backing array. Whereas, list
will fall back to using an iterator to add all the elements.
In [2]: arr = numpy.arange(1000)
In [3]: %timeit arr.tolist()
10000 loops, best of 3: 33 µs per loop
In [4]: %timeit list(arr)
10000 loops, best of 3: 80.7 µs per loop
I would expect the tolist
to be