After some complex operations, a resultant list is obtained, say list1, which is a list of different arrays.
Following is the list1
In [] : list1
Out [] :
[array([ 10.1]),
array([ 13.26]),
array([ 11.0 , 12.5])]
Want to convert this list to simple list of lists and not arrays
Expected list2
[ [ 10.1],
[ 13.26],
[ 11.0 , 12.5] ]
Please let me know if anything is unclear.
array ] objects can be converted to a list with the tolist() function. The tolist() function doesn't accept any arguments. If the array is one-dimensional, a list with the array elements is returned. For a multi-dimensional array, a nested list is returned.
To convert a NumPy array (ndarray) to a Python list use ndarray. tolist() function, this doesn't take any parameters and returns a python list for an array. While converting to a list, it converts the items to the nearest compatible built-in Python type.
You can use tolist()
in a list comprehension:
>>> [l.tolist() for l in list1]
[[0.0], [0.0], [0.0, 0.5], [0.5], [0.5], [0.5, 0.69], [0.69, 0.88], [0.88], [0.88], [0.88], [0.88, 1.0], [1.0, 1.1], [1.1], [1.1], [1.1], [1.1, 1.5], [1.5, 2.0], [2.0], [2.0]]
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