Slicing works differently with NumPy arrays. The NumPy docs devote a lengthy page on the topic. To highlight some points:
- NumPy slices can slice through multiple dimensions
- All arrays generated by NumPy basic slicing are always views of the original array, while slices of lists are shallow copies.
- You can assign a scalar into a NumPy slice.
- You can insert and delete items in a
list
by assigning a sequence of different length to a slice, whereas NumPy would raise an error.
Demo:
>>> a = np.arange(4, dtype=object).reshape((2,2))
>>> a
array([[0, 1],
[2, 3]], dtype=object)
>>> a[:,0] #multidimensional slicing
array([0, 2], dtype=object)
>>> b = a[:,0]
>>> b[:] = True #can assign scalar
>>> a #contents of a changed because b is a view to a
array([[True, 1],
[True, 3]], dtype=object)
Also, NumPy arrays provide convenient mathematical operations with arrays of objects that support them (e.g. fraction.Fraction
).