I have a question about how to create a sublist (I hope this is the right term to use) from a given list without copying.
It seems that slicing can create sublists, but
numpy
's array objects support this notion of creating interdependent sub-lists, by having slicing return views
rather than copies of the data.
Altering the original numpy
array will alter the views created from the array, and changes to any of the views will also be reflected in the original array. Especially for large data sets, views are a great way of cutting data in different ways, while saving on memory.
>>> import numpy as np
>>> array1 = np.array([1, 2, 3, 4])
>>> view1 = array1[1:]
>>> view1
array([2, 3, 4])
>>> view1[1] = 5
>>> view1
array([2, 5, 4])
>>> array1
array([1, 2, 5, 4]) # Notice that the change to view1 has been reflected in array1
For further reference, see the numpy documentation on views as well as this SO post.