Skip to content

Commit d89b4ac

Browse files
committed
Add implementation of dpnp.ndarray.tolist method
1 parent 5b39d66 commit d89b4ac

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

dpnp/dpnp_array.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,51 @@ def to_device(self, device, /, *, stream=None):
20142014

20152015
# 'tobytes',
20162016
# 'tofile',
2017-
# 'tolist',
2017+
2018+
def tolist(self):
2019+
"""
2020+
Converts the array to a (possibly nested) Python list.
2021+
2022+
For full documentation refer to :obj:`numpy.ndarray.tolist`.
2023+
2024+
Returns
2025+
-------
2026+
out : list
2027+
The possibly nested Python list of array elements.
2028+
2029+
Examples
2030+
--------
2031+
For a 1D array, ``a.tolist()`` is almost the same as ``list(a)``,
2032+
except that ``tolist`` changes 0-d arrays to Python scalars:
2033+
2034+
>>> import numpy as np
2035+
>>> a = np.array([1, 2])
2036+
>>> list(a)
2037+
[array(1), array(2)]
2038+
>>> a_tolist = a.tolist()
2039+
[1, 2]
2040+
2041+
Additionally, for a 2D array, ``tolist`` applies recursively:
2042+
2043+
>>> a = np.array([[1, 2], [3, 4]])
2044+
>>> list(a)
2045+
[array([1, 2]), array([3, 4])]
2046+
>>> a.tolist()
2047+
[[1, 2], [3, 4]]
2048+
2049+
The base case for this recursion is a 0D array:
2050+
2051+
>>> a = np.array(1)
2052+
>>> list(a)
2053+
Traceback (most recent call last):
2054+
...
2055+
TypeError: iteration over a 0-d array
2056+
>>> a.tolist()
2057+
1
2058+
2059+
"""
2060+
2061+
return self.asnumpy().tolist()
20182062

20192063
def trace(self, offset=0, axis1=0, axis2=1, dtype=None, *, out=None):
20202064
"""

0 commit comments

Comments
 (0)