What is the difference between applying list()
on a numpy
array vs. calling tolist()
?
I was checking the types of both outputs and they both show that what I'm getting as a result is a list
, however, the outputs don't look exactly the same. Is it because that list()
is not a numpy
-specific method (i.e. could be applied on any sequence) and tolist()
is numpy
-specific, and just in this case they are returning the same thing?
Input:
points = numpy.random.random((5,2))
print "Points type: " + str(type(points))
Output:
Points type: <type 'numpy.ndarray'>
Input:
points_list = list(points)
print points_list
print "Points_list type: " + str(type(points_list))
Output:
[array([ 0.15920058, 0.60861985]), array([ 0.77414769, 0.15181626]), array([ 0.99826806, 0.96183059]), array([ 0.61830768, 0.20023207]), array([ 0.28422605, 0.94669097])]
Points_list type: 'type 'list''
Input:
points_list_alt = points.tolist()
print points_list_alt
print "Points_list_alt type: " + str(type(points_list_alt))
Output:
[[0.15920057939342847, 0.6086198537462152], [0.7741476852713319, 0.15181626186774055], [0.9982680580550761, 0.9618305944859845], [0.6183076760274226, 0.20023206937408744], [0.28422604852159594, 0.9466909685812506]]
Points_list_alt type: 'type 'list''
While Python lists store a collection of ordered, alterable data objects, NumPy arrays only store a single type of object. So, we can say that NumPy arrays live under the lists' umbrella. Therefore, there is nothing NumPy arrays do lists do not.
tolist(), used to convert the data elements of an array into a list. This function returns the array as an a. ndim- levels deep nested list of Python scalars. In simple words, this function returns a copy of the array elements as a Python list.
1. NumPy uses much less memory to store data. The NumPy arrays takes significantly less amount of memory as compared to python lists. It also provides a mechanism of specifying the data types of the contents, which allows further optimisation of the code.
List is used to collect items that usually consist of elements of multiple data types. An array is also a vital component that collects several items of the same data type. List cannot manage arithmetic operations. Array can manage arithmetic operations.
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 'list'>
>>> type(a.tolist())
<type 'list'>
the elements of each list have a different type:
>>> type(list(a)[0])
<type 'numpy.ndarray'>
>>> type(a.tolist()[0])
<type 'list'>
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.
.tolist()
appears to convert all of the values recursively to python primitives (list
), whereas list
creates a python list from an iterable. Since the numpy array is an array of arrays
, list(...)
creates a list
of array
s
You can think of list
as a function that looks like this:
# Not the actually implementation, just for demo purposes
def list(iterable):
newlist = []
for obj in iter(iterable):
newlist.append(obj)
return newlist
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With