In [232]: alist = [1, 1, 1, [1,2], 1,[3,4,5],[6,7]]
As of 1.19 numpy warns about making an array from such as list:
In [233]: np.array(alist)
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
#!/usr/bin/python3
Out[233]:
array([1, 1, 1, list([1, 2]), 1, list([3, 4, 5]), list([6, 7])],
dtype=object)
Notice that this array contains numbers and lists:
In [234]: np.array(alist, object)
Out[234]:
array([1, 1, 1, list([1, 2]), 1, list([3, 4, 5]), list([6, 7])],
dtype=object)
For an object dtype array, it iterates (at python speed) over the elements, and delegates the action to the element's own method. For a list *5 means replicate:
In [235]: np.array(alist, object)*5
Out[235]:
array([5, 5, 5, list([1, 2, 1, 2, 1, 2, 1, 2, 1, 2]), 5,
list([3, 4, 5, 3, 4, 5, 3, 4, 5, 3, 4, 5, 3, 4, 5]),
list([6, 7, 6, 7, 6, 7, 6, 7, 6, 7])], dtype=object)
Now if the list contained arrays instead of lists:
In [236]: alist = [1, 1, 1, np.array([1,2]), 1,np.array([3,4,5]),np.array([6,7])]
In [237]: np.array(alist, object)
Out[237]:
array([1, 1, 1, array([1, 2]), 1, array([3, 4, 5]), array([6, 7])],
dtype=object)
now the multiplication works for the array elements - with the numeric multiply.
In [238]: np.array(alist, object)*5
Out[238]:
array([5, 5, 5, array([ 5, 10]), 5, array([15, 20, 25]), array([30, 35])],
dtype=object)
Math on object dtype arrays is hit-or-miss, depending on the type of the elements. It works for some, not for others, depending on how the operation is implemented by the elements. And even where it works, the speed is more like a list comprehension than a multidimensional array.
In [244]: timeit np.array(alist, object)*5
13.3 µs ± 333 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [245]: timeit [i*5 for i in alist]
6.43 µs ± 140 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [246]: timeit np.array([i*5 for i in alist],object)
9.98 µs ± 47.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)