Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions cf/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12498,26 +12498,33 @@ def tan(self, inplace=False, i=False):

return d

@daskified(_DASKIFIED_VERBOSE)
def tolist(self):
"""Return the array as a (possibly nested) list.
"""Return the data as a scalar or (nested) list.

Returns the data as an ``N``-levels deep nested list of Python
scalars, where ``N`` is the number of data dimensions.

Return a copy of the array data as a (nested) Python list. Data
items are converted to the nearest compatible Python type.
If ``N`` is 0 then, since the depth of the nested list is 0,
it will not be a list at all, but a simple Python scalar.

:Returns:

`list`
The possibly nested list of array elements.
`list` or scalar
The (nested) list of array elements, or a scalar if
the data has 0 dimensions.

**Examples**

>>> d = cf.Data(9)
>>> d.tolist()
9

>>> d = cf.Data([1, 2])
>>> d.tolist()
[1, 2]

>>> d = cf.Data(([[1, 2], [3, 4]]))
>>> list(d)
[array([1, 2]), array([3, 4])] # DCH CHECK
>>> d.tolist()
[[1, 2], [3, 4]]

Expand Down
7 changes: 7 additions & 0 deletions cf/test/test_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3927,6 +3927,13 @@ def test_Data_set_units(self):
with self.assertRaises(ValueError):
d.set_units("km")

def test_Data_tolist(self):
for x in (1, [1, 2], [[1, 2], [3, 4]]):
d = cf.Data(x)
e = d.tolist()
self.assertEqual(e, np.array(x).tolist())
self.assertTrue(d.equals(cf.Data(e)))


if __name__ == "__main__":
print("Run date:", datetime.datetime.now())
Expand Down