Pablo S.
Pablo S.

Reputation: 41

Is there any difference between list(array) and array.tolist() in Python?

The following variables seem to be similar but they aren't and I don't understand why:

import ujson
import numpy as np

arr = np.array([1, 2, 3, 4])
arr_1 = arr.tolist()
arr_2 = list(arr)

arr_1 == arr_2  # True

ujson.dumps({'arr': arr_1})  # it works
ujson.dumps({'arr': arr_2})  # it does not work (OverflowError: Maximum recursion level reached)

I am using Python-3.5.6, ujson-1.35 and numpy-1.16.4.

Thank you a lot for your help!!

Upvotes: 4

Views: 207

Answers (2)

swapnil choudhari
swapnil choudhari

Reputation: 1

The key differences between tolist() and list() are

The tolist() method converts a multidimensional array into a nested list whereas list() converts it to a list of arrays. For example,

import numpy as np

# create a 2-D array
array1 = np.array([[1, 2], [3, 4]])

# convert a 2-D array to nested list
list1 = array1.tolist()

# convert a 2-D array to a list of arrays
list2 = list(array1)

print('Using array.tolist(): ', list1)
print('Using list(array): ', list2)

Upvotes: 0

Stuart
Stuart

Reputation: 9858

numpy has its own numeric data types for different levels of precision.

These are created in ways that allow easy comparison with regular Python integers.

np.int32(3) == 3   # True
[np.int32(1), 4] == [1, np.int32(4)]    # True

(Lists are equal in Python if all elements at the same index are equal)

That is why your arr_1 == arr_2.

They cannot readily be serialized to json, but the tolist method converts them to regular Python numeric types which do allow serialization.

Upvotes: 5

Related Questions