What is the difference between applying list() on a numpy array vs. calling tolist()?
I was checking the types of both output
Your example already shows the difference; consider the following 2D array:
>>> import numpy as np
>>> a = np.arange(4).reshape(2, 2)
>>> a
array([[0, 1],
[2, 3]])
>>> a.tolist()
[[0, 1], [2, 3]] # nested vanilla lists
>>> list(a)
[array([0, 1]), array([2, 3])] # list of arrays
tolist handles the full conversion to nested vanilla lists (i.e. list of list of int), whereas list just iterates over the first dimension of the array, creating a list of arrays (list of np.array of np.int64). Although both are lists:
>>> type(list(a))
>>> type(a.tolist())
the elements of each list have a different type:
>>> type(list(a)[0])
>>> type(a.tolist()[0])
The other difference, as you note, is that list will work on any iterable, whereas tolist can only be called on objects that specifically implement that method.