October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

NumPy Shape in Python: What `shape[0]` and `shape[1]` Mean

For a 2-D NumPy array, shape is (rows, columns): shape[0] gives rows and shape[1] gives columns. Learn how tuple indexing changes across dimensions.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a two-dimensional NumPy array, array.shape is a tuple in the order (rows, columns). That means array.shape[0] is the row count and array.shape[1] is the column count. The values are tuple entries, not special NumPy methods.

What does a NumPy array’s shape tuple mean?

NumPy’s ndarray.shape is a tuple of non-negative integers: each entry gives the length of the array along one axis. In a two-dimensional, matrix-like array, those axis lengths are conventionally read as rows followed by columns. NumPy’s ndarray documentation shows a 2-by-3 array with shape (2, 3).

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

print(arr.shape)     # (2, 3)
print(arr.shape[0])  # 2 rows
print(arr.shape[1])  # 3 columns

Python tuples use zero-based indexing, so index 0 selects the first entry and index 1 selects the second. NumPy’s beginner guide also explains how to inspect an array’s shape and size.

When are shape[0] and shape[1] valid?

A shape tuple has one entry per dimension, so an index is valid only if that position exists.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • A one-dimensional array with four elements has shape (4,). Its shape has only one entry: shape[0] is valid, but shape[1] raises IndexError. The trailing comma is Python’s notation for a one-item tuple.
  • A two-dimensional array has two entries, so shape[0] and shape[1] are valid.
  • A three-dimensional array with shape (2, 3, 4) has three axis lengths, accessible as shape[0], shape[1], and shape[2].

If an input’s dimensionality may vary, check arr.ndim before accessing a particular axis, or test whether len(arr.shape) is large enough. NumPy documents that arr.ndim equals len(arr.shape); see its shape reference.

How shape differs from ndim and size

These attributes answer different questions:

  • shape gives the length along each axis as a tuple.
  • ndim gives the number of axes, which is also the number of entries in shape.
  • size gives the total number of elements. For shape (3, 4), the array has 12 elements.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What happens to shape when you transpose an array?

Transposing a two-dimensional array swaps its axes. For example, an array with shape (3, 4) has shape (4, 3) after its rows and columns are exchanged. NumPy illustrates this in its quickstart guide. This is why a shape entry describes an axis position, rather than a permanent label attached to the underlying data.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.