Reshaping and Flattening

Reshaping is rearranging the same array elements into a new layout of rows, columns, and dimensions, while flattening is collapsing a multi-dimensional array back down into a single 1D line — neither changes the underlying values.

Learn Reshaping and Flattening in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll reshape with explicit sizes, let NumPy infer a dimension with -1 , flatten with ravel() and flatten() , transpose with .T , and add a new axis.

reshape(rows, cols) takes a flat array and folds its elements into a new grid, filling row by row. The data stays the same — only the arrangement changes. The new shape must hold exactly as many elements as the original.

Expected output: a 3×4 grid of 0–11, then (3, 4) .

Pass -1 for one dimension and NumPy figures out its size from the total element count. Useful when you know you want, say, 3 rows but don't want to compute the column count yourself.

Expected output: (3, 4) , (6, 2) , and (12,) .

Both ravel() and flatten() collapse a multi-dimensional array into 1D. The difference is memory: ravel() returns a view (shares memory with the original when possible), while flatten() always returns an independent copy .

Expected output: [1 2 3 4 5 6] , then 99 (ravel changed the source) and 1 (flatten left it alone).

.T transposes an array — it flips rows and columns so the element at [i, j] moves to [j, i] . To add a brand-new axis (e.g. turn a 1D array into a single column), use np.newaxis or an equivalent reshape .

Expected output: the 3×2 transpose, (2, 3) (3, 2) , then the column array and (3, 1) .

Replace each ___ so the program reshapes, infers a dimension, and transposes.

Expected output: (2, 4) , (4, 2) , and the transposed grid. (Answers: 4 , -1 , T .)

❌ ValueError: cannot reshape array of size 12 into shape (5,5)

The new shape needs a different number of elements than the array holds.

✅ Fix: make rows × cols equal the size. 12 fits (3, 4) or (2, 6), not (5, 5).

❌ Editing a flattened array changed the original

You used ravel() , which can return a view that shares memory.

✅ Fix: use flatten() when you want an independent copy.

Reshape a range into a 3×4 grid, flatten it back, and transpose the grid — all without changing the values.

Lesson 8 complete — you can reshape anything!

You now reshape with explicit sizes, let NumPy infer a dimension with -1 , flatten with ravel() and flatten() (knowing the view-vs-copy difference), transpose with .T , and add new axes.

🚀 Up next: Universal Functions — fast, element-wise math built into NumPy.

Practice quiz

What does arr.reshape(2, 3) do to a 6-element array?

  • Arranges it into 2 rows of 3 columns
  • Sorts it
  • Removes 3 elements
  • Doubles its values

Answer: Arranges it into 2 rows of 3 columns. reshape rearranges the same elements into a 2-row, 3-column grid without changing the data.

What does passing -1 for one dimension in reshape mean?

  • Reverse the array
  • Let NumPy compute that size from the total count
  • Remove a dimension
  • Use negative indexing

Answer: Let NumPy compute that size from the total count. -1 tells NumPy to infer that dimension's size from the total number of elements.

What does arr.reshape(3, -1) give for a 12-element array?

  • Shape (12, 1)
  • An error
  • Shape (3, 4)
  • Shape (4, 3)

Answer: Shape (3, 4). With 12 elements and 3 rows, NumPy computes 4 columns, giving (3, 4).

What is the key difference between ravel() and flatten()?

  • ravel sorts, flatten does not
  • They are identical
  • ravel works only on 1D
  • ravel may return a view; flatten always returns a copy

Answer: ravel may return a view; flatten always returns a copy. ravel returns a view sharing memory when possible; flatten always returns an independent copy.

If you edit a ravel() result, what can happen to the original?

  • It may change because they can share memory
  • It is always safe
  • It gets deleted
  • It gets sorted

Answer: It may change because they can share memory. ravel may share memory, so editing the result can change the original array.

How many -1 dimensions can a single reshape call have?

  • As many as you want
  • Only one
  • Two
  • Zero

Answer: Only one. NumPy can solve for only one unknown dimension, so only one -1 is allowed.

What does .T do to a 2D array?

  • Truncates it
  • Totals the elements
  • Transposes rows and columns
  • Tiles it

Answer: Transposes rows and columns. .T transposes, moving the element at [i, j] to [j, i].

Why does reshaping a size-12 array into (5, 5) fail?

  • 5 is prime
  • (5, 5) needs 25 elements, not 12
  • reshape needs square inputs
  • It needs a -1

Answer: (5, 5) needs 25 elements, not 12. Reshape must preserve the element count; (5, 5) requires 25 elements.

Which flattening method always gives an independent copy?

  • ravel()
  • reshape(-1)
  • flatten()
  • .T

Answer: flatten(). flatten() always returns an independent copy you can edit safely.

What shape does arr.reshape(-1, 2) give for a 12-element array?

  • (6, 2)
  • (2, 6)
  • (12, 2)
  • (2, 12)

Answer: (6, 2). With 2 columns and 12 elements, NumPy computes 6 rows, giving (6, 2).