Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.diag

Extract a selected diagonal from a 2-D array or build a 2-D diagonal array from 1-D values. Use k to choose an offset from the main diagonal.

How it works

For a two-dimensional input, the function extracts the selected diagonal as a one-dimensional array. For a one-dimensional input, it creates a two-dimensional array containing those values on the selected diagonal and fills the remaining positions with zeros.

Syntax

numpy.diag(v, k=0)

Parameters

NameTypeDescription
varray_likeAn array-like object. If it is two-dimensional, the selected diagonal is extracted; if it is one-dimensional, a two-dimensional diagonal array is constructed.
kint, optionalThe diagonal offset. The default is 0; positive values select diagonals above the main diagonal and negative values select diagonals below it.

Returns

out ndarray: the extracted diagonal or constructed diagonal array.

Key points

  • np.diag supports two complementary operations: extracting a diagonal from a matrix and constructing a matrix from a one-dimensional sequence.
  • A two-dimensional input produces the selected diagonal as a one-dimensional result.
  • A one-dimensional input produces a two-dimensional array with the input values placed along the requested diagonal.
  • Leaving k unspecified operates on the main diagonal.
  • The sign of k chooses the diagonal's position relative to the main diagonal.
  • The function's output is an ndarray representing either the selected diagonal or the newly constructed diagonal matrix.

Common mistakes

  • When extracting a diagonal, do not assume a stable copy-versus-view behavior across NumPy versions; use numpy.diagonal when writable extraction is required.

Example: Extract and construct diagonals

import numpy as np

matrix = np.arange(9).reshape(3, 3)
print(np.diag(matrix, k=1))
print(np.diag([10, 20, 30], k=-1))

The first call extracts the diagonal above the main diagonal; the second constructs a matrix with the values below the main diagonal.

When to use it

Diagonal extraction

Read a matrix's main or offset diagonal for inspection or computation.

Diagonal matrix construction

Create a square or offset-diagonal matrix from a vector of values.

Diagonal isolation

Round-trip a matrix diagonal by extracting it and constructing a diagonal-only matrix.

Interview questions

QHow does np.diag behave for one-dimensional versus two-dimensional input?

With a two-dimensional input, np.diag returns the specified diagonal as a one-dimensional array. With a one-dimensional input, it places the values on the requested diagonal of a two-dimensional result.

Follow-up: How does k select the diagonal? Positive k selects diagonals above the main diagonal, while negative k selects those below it.

QWhat does the k parameter control?

The k parameter identifies which diagonal is involved, and its default is 0, the main diagonal. A positive value moves above that diagonal; a negative value moves below it.

Follow-up: What happens when k is omitted? The main diagonal is selected or constructed because k defaults to 0.

QWhat does np.diag return?

The function returns an ndarray containing either the extracted diagonal or the constructed diagonal matrix. Its result shape therefore depends on whether v is two-dimensional or one-dimensional.

Follow-up: How can a diagonal matrix be made from an existing matrix's diagonal? Extract it first with np.diag, then pass that one-dimensional result back to np.diag.

  • numpy.diagonal
  • numpy.diagflat

Source: numpy.diag. Checked 2026-10-15.