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

NumPy · Function

np.ravel

Flatten an array-like input into a contiguous one-dimensional array. The reading order is configurable, and copying occurs only when needed.

How it works

np.ravel reads an input array in the requested index order and produces a contiguous one-dimensional array. It can reuse the input storage when possible, but creates a copy when contiguity or the requested ordering requires it.

Syntax

numpy.ravel(a, order='C')

Parameters

NameTypeDescription
aarray_likeInput array or array-like object whose elements will be flattened.
order{'C', 'F', 'A', 'K'}, optionalIndexing order used to read the input: 'C', 'F', 'A', or 'K'. It defaults to 'C'.

Returns

A contiguous one-dimensional array of the same subtype as a, with shape (a.size,).

Key points

  • The function converts an array-like input into a contiguous one-dimensional result.
  • Every input element appears in the returned one-dimensional array.
  • Traversal order is selected independently from the output shape.
  • Order 'A' adapts to whether the input uses Fortran-contiguous storage.
  • Order 'K' follows the array's actual memory arrangement.

Common mistakes

  • If order is omitted, elements are flattened in C-style row-major order.
  • With order='K', negative strides can cause the memory-order traversal to reverse data.

Example: Flatten an array

import numpy as np

a = np.array([[1, 2], [3, 4]])
print(np.ravel(a))       # [1 2 3 4]
print(np.ravel(a, order="F"))  # [1 3 2 4]

Flatten a two-dimensional array using the default row-major order and then column-major order.

When to use it

Convert data to one dimension

Prepare multidimensional data for APIs or algorithms that require a single sequence.

Control element order

Choose row-major or column-major traversal before serialization or processing.

Prefer a view when possible

Reduce unnecessary allocation when a contiguous view can satisfy the requested layout.

Interview questions

QWhat does np.ravel return, and when does it copy?

np.ravel returns a contiguous one-dimensional array containing all input elements. It makes a copy only when necessary, so it may be more memory-efficient than an operation that always copies.

Follow-up: How does order='K' differ from order='C'? order='K' follows the elements' memory layout, while order='C' uses row-major indexing regardless of the layout.

QExplain the available order values.

The order argument controls how elements are read before they are packed into one dimension. 'C' uses row-major order, 'F' uses column-major order, 'A' chooses Fortran-like order for Fortran-contiguous input and C-like order otherwise, and 'K' follows memory order.

Follow-up: Which order should be used to preserve the physical traversal of an array? Use 'K', subject to its documented handling of negative strides.

QWhat are the shape and subtype of the result?

The result is a contiguous one-dimensional array with shape (a.size,). It has the same subtype as the input array, preserving ndarray subclasses where applicable.

Follow-up: Does flattening change the number of elements? No. It changes the shape to one dimension while retaining a total element count equal to the input's size.

  • numpy.ndarray.flatten
  • numpy.reshape
  • numpy.ndarray.ravel

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