Make the following 4x7 array called nola
that starts with 1 and steps by 2. However, note that the first element in each row is always 4 more than the last element in the previous row.
Solution¶
Explanation
The trick here is to think of nola
as the first 7 columns in this 4x8 array.
This array is simply the sequence 1, 3, 5, ... 61, 63 (without any weird gaps) arranged into a 4x8 shape.
So, if we can build this array, we can easily chop off the last column to make nola
.

Build the sequence array:
1, 3, 5, ... 61, 63
.Here we use
np.arange()
, passing instart=1
,stop=65
, andstep=2
. Note thatstop
is exclusive, so the sequence goes from 1 up to but not including 65. 
Reshape the 1D array into a 2D array.
Specifically, we convert the array from shape
(32,)
to shape(4,8)
. There are a few ways we can do this..See
numpy.reshape()
.See
ndarray.reshape()
.Pro Tip
In either of these methods, we can replace exactly one of the dimensions with 1 and NumPy will figure it out for us. For example,
Here we're basically telling NumPy, "reshape the array into an array with 4 rows". Since the original array had 32 elements, NumPy knows the resulting array must have 8 columns.

Select all columns but the last.
nola[:, :1]
can be interpreted as "Select every row, and select all columns from the start, up to but excluding the last column". (1
means "last" index. See negative indexing.)