Typefully

Why NumPy arrays are not "sequences", technically

Avatar

Share

 • 

3 years ago

 • 

View on X

Numpy arrays look and act very much like other Python sequences… …but they're technically not sequences They're iterable. They're containers. They're sized. But they're not sequences
A sequence is a collection in which you can access an element using an integer index You can do this with NumPy arrays: >>> some_array = np.array([3, 6, 9]) >>> some_array[1] 6
But you can also use more advanced indexing, such as: >>> some_array = np.array([[1, 2, 3], [4, 5, 6]]) >>> some_array array([[1, 2, 3], [4, 5, 6]]) >>> some_array[1, 2] 6
You use two indices to index the rows and columns Here's more in indexing in NumPy in this 2-day series: twitter.com/s_gruppetta_ct/status/1624034209936609281
However, there's another important difference you should be aware of when moving from using lists to NumPy arrays Here's an example to show the difference in behaviour:
Let's start with a list: >>> numbers = [2, 4, 6, 8, 10] …and get a slice from that list: >>> numbers_subset = numbers[2:5] >>> numbers_subset [6, 8, 10]
Change a value from the slice: >>> numbers_subset[-1] = 999 >>> numbers_subset [6, 8, 999] As you'd expect, the last value of `numbers_subset` has changed, but…
The original list has _not_ changed: >>> numbers [2, 4, 6, 8, 10] When you create a slice of a list or any sequence, you're creating a _copy_ of the data
Let's replicate this with NumPy arrays: >>> import numpy as np >>> numbers = np.array([2, 4, 6, 8, 10]) >>> numbers_subset = numbers[2:5] >>> numbers_subset array([ 6, 8, 10]) You create an array and a slice of that array. So far, nothing _looks_ different
You can change the value of one of the elements from the slice: >>> numbers_subset[-1] = 999 >>> numbers_subset array([ 6, 8, 999]) This should be what you were expecting, too. But…
If you look at `number`, which is the original array, you'll note different behaviour from when you used lists: >>> numbers array([ 2, 4, 6, 8, 999]) The original array has also changed when you changed a value in the slice
You made the change in `numbers_subset` but the value also changed in `numbers` Why?
When you create a slice on a NumPy array, you create a "view" and not a "copy" of the array – you're still using the same data in the original array You can read more on NumPy views here (in this rather old thread…): twitter.com/s_gruppetta_ct/status/1522301373592653824
And if you want to make sure you understand the difference between iterable, collection, sequence, and other terms we use for data structures, you can see this series: twitter.com/s_gruppetta_ct/status/1628341581538549760
Avatar

Stephen Gruppetta

@s_gruppetta_ct

Constantly looking for innovative ways to talk and write about Python • Mentoring learners • Writing about Python • Writing about technical writing