Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

reshape() gives a NumPy array a new shape without changing its values. Use arr.reshape(new_shape) or np.reshape(arr, new_shape); the requested dimensions must contain exactly the same number of elements, although one dimension may be -1 so NumPy can infer it. The default traversal is C order, and the result may be a view or a copy depending on the array’s layout and the requested order.

This guide covers shape arithmetic, rows and columns, C/F/A order, inferred dimensions, views versus copies, current NumPy arguments, common errors, and practical patterns.

How do I reshape a NumPy array?

Import NumPy, create or obtain an array, then call its reshape() method:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import numpy as np

arr = np.arange(6)
reshaped = arr.reshape(3, 2)

print(reshaped)
# [[0 1]
#  [2 3]
#  [4 5]]
print(reshaped.shape)  # (3, 2)

The top-level function is equivalent:

reshaped = np.reshape(arr, (3, 2))

NumPy’s reference describes reshape as giving “a new shape to an array without changing its data.” The original object is not reshaped in place; assign the returned array if you need to keep it.

Current NumPy documentation lists numpy.reshape(a, /, shape=None, order='C', *, newshape=None, copy=None). Use shape in new code. The older newshape keyword has been deprecated since NumPy 2.1, although it remains for compatibility.

How do I reshape an array to rows and columns?

Multiply the requested dimensions and compare that product with arr.size. A six-element array can become 2×3, 3×2, 1×6, or 6×1:

x = np.arange(6)

rows_columns = x.reshape(2, 3)
print(rows_columns)
# [[0 1 2]
#  [3 4 5]]

one_row = x.reshape(1, 6)
one_column = x.reshape(6, 1)

The shape tuple is explicit and easy to read, but the method also accepts dimensions as separate positional arguments:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
x.reshape(2, 3)       # same as x.reshape((2, 3))
x.reshape(1, 6)

Reshape does not pad, truncate, or reorder values arbitrarily. A request whose dimensions multiply to a different number raises a ValueError:

x.reshape(4, 2)       # ValueError: 6 elements cannot fill 8 positions

Check the element count before reshaping

target = (3, 4)
if np.prod(target) != x.size:
    raise ValueError("target shape has the wrong number of elements")

arr.size is the total number of elements, while arr.shape reports the size of each axis.

How does NumPy reshape infer -1?

Put -1 in one dimension when you know the other dimensions but want NumPy to calculate the remaining size. For six values, (3, -1) means (3, 2):

x = np.arange(6)
print(x.reshape(3, -1).shape)  # (3, 2)
print(x.reshape(2, -1))
# [[0 1 2]
#  [3 4 5]]

For 30 values, (2, -1, 3) infers 5 because 2 × 5 × 3 = 30:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
cube = np.arange(30).reshape(2, -1, 3)
print(cube.shape)  # (2, 5, 3)

Only one dimension may be -1; NumPy cannot infer two unknown dimensions. The known dimensions still must divide the element count exactly.

What does order='C' mean in NumPy reshape?

order specifies how NumPy reads values from the input and places them in the output. The default, 'C', uses row-style indexing: the last index changes fastest.

x = np.array([[0, 1],
              [2, 3],
              [4, 5]])

print(np.reshape(x, (2, 3)))
# [[0 1 2]
#  [3 4 5]]

In this example, the values are traversed as 0, 1, 2, 3, 4, 5 and then placed into two rows of three.

What does order='F' mean?

'F' uses column-style indexing: the first index changes fastest during traversal. It is useful when matching a Fortran-style indexing convention or a data source documented that way.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
print(np.reshape(x, (2, 3), order='F'))
# [[0 4 3]
#  [2 1 5]]

Do not interpret order='F' as a guarantee that the result’s physical memory layout is Fortran-contiguous. C and F describe indexing order for the reshape operation; the returned array’s contiguity is not guaranteed.

When should you use order='A'?

'A' uses Fortran indexing if the input is Fortran-contiguous and C indexing otherwise. It can preserve the input’s existing convention when code handles arrays from multiple sources. If you simply need ordinary Python/NumPy row traversal, leave the default 'C'.

Does NumPy reshape return a view or a copy?

It can return either. NumPy creates a view when the existing strides and requested order allow the new shape without moving data; otherwise it allocates a copy. Therefore, never assume reshape is always zero-copy or that the result always owns independent storage.

a = np.arange(6)
b = a.reshape(2, 3)
b[0, 0] = 99
print(a)  # often shows 99 because this reshape can be a view

The example demonstrates possible sharing, not a universal promise for every array. Sliced, transposed, or otherwise non-contiguous inputs can require a copy.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Control copying with copy

With the current function signature, copy=None copies only when required by the requested order, copy=True always permits and requests a copy, and copy=False raises ValueError if a copy cannot be avoided.

a = np.arange(6)
view_or_copy = np.reshape(a, (2, 3), copy=None)
independent = np.reshape(a, (2, 3), copy=True)
# strict_view raises ValueError if this particular operation needs copying
strict_view = np.reshape(a, (2, 3), copy=False)

For an actual array, inspect sharing rather than inferring it from the call:

np.shares_memory(a, view_or_copy)

Reshape versus transpose, resize, and ravel

  • reshape: returns an array with a different shape while preserving the traversal of values selected by order.
  • transpose or .T: permutes axes. It changes which axis is first, second, and so on; it is not a substitute for reshaping.
  • ravel: flattens an array to one dimension when possible. You can then reshape that one-dimensional traversal.
  • ndarray.resize: changes shape and size in place, potentially repeating or truncating data. It is a different operation from reshape.
matrix = np.arange(6).reshape(2, 3)
print(matrix.T.shape)            # (3, 2)
print(matrix.reshape(3, 2).shape) # (3, 2), but values are grouped differently
print(matrix.ravel().shape)       # (6,)

Practical reshape patterns

Convert a flat batch into rows

records = np.arange(20)
rows = records.reshape(-1, 4)
print(rows.shape)  # (5, 4)

This is useful when each record has four fields and the total length is divisible by four.

Add or remove a singleton axis

values = np.arange(4)
column = values.reshape(-1, 1)  # (4, 1)
row = values.reshape(1, -1)     # (1, 4)

These forms are common when broadcasting requires an explicit row or column dimension.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reshape multidimensional data

image = np.arange(24).reshape(2, 3, 4)
flat = image.reshape(-1)
restored = flat.reshape(2, 3, 4)
assert np.array_equal(image, restored)

Troubleshooting reshape errors

“cannot reshape array of size … into shape …”

The target product does not equal arr.size. Print both and correct the dimensions, or use one -1 for the unknown dimension.

“can only specify one unknown dimension”

You supplied more than one -1. Calculate all but one dimension yourself.

Unexpected value arrangement

You may need a different traversal order, or you may actually want transpose. Compare a small known array under C and F order before processing production data.

Changes unexpectedly affect the source

The result may share memory with the input. Use copy=True when independent storage is required, and verify with np.shares_memory().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reshape is slower than expected

A copy may be necessary for a non-contiguous input or requested order. Avoid unnecessary transposes and copies, and choose copy=False when enforcing a no-copy requirement is more important than accepting an exception.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance, reliability, and API choices

Reshape itself is usually inexpensive when a view is possible, but copying can scale with the number of elements. For large arrays, check contiguity and memory sharing before placing reshape inside a hot loop. Keep shape calculations explicit at data boundaries, validate divisibility, and document whether C or F traversal matches the producer’s format.

The method form reads naturally when you already have an array: arr.reshape(...). The function form is convenient in generic code that receives an array-like object: np.reshape(arr, ...). Both express the same operation. Prefer the modern shape argument over deprecated newshape.

Or skip the browser setup

If your workflow also needs webpage images for documentation, test fixtures, or model context, ScreenshotNeo provides a single screenshot API request instead of configuring a headless browser. It removes cookie/consent banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, with the result identified by response headers. Its MCP server exposes take_screenshot, get_page_info, and capture_pdf to Claude, Cursor, and other MCP clients.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use the API documentation at screenshotneo.com/docs/ for all options. A one-call cURL example is:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots. Create a free ScreenshotNeo account.

Further reading

Frequently Asked Questions

Can reshape change an array’s data type?

No. Reshape changes the dimensions and indexing of the array; use an explicit dtype conversion such as astype() when you need a different data type.

Can I reshape an empty array?

Yes, but the target shape must still be compatible with zero elements. A dimension inferred with -1 must be mathematically determinable from the remaining dimensions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Is reshape() available on Python lists?

Python lists do not have this method. Convert first with np.asarray(your_list) or np.array(your_list).

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.