NumPy ndarray.flatten()

The numpy.ndarray.flatten() method returns a copy of the original array collapsed into a one-dimensional array. It provides flexibility in choosing the order in which the elements are read from the array.

Syntax

</>
Copy
ndarray.flatten(order='C')

Parameters

ParameterTypeDescription
order{‘C’, ‘F’, ‘A’, ‘K’}, optionalSpecifies the order in which elements are read. Default is 'C' (row-major). Other options:
  • 'C': Reads elements row-wise (C-style).
  • 'F': Reads elements column-wise (Fortran-style).
  • 'A': Uses column-major if the array is Fortran-contiguous, otherwise row-major.
  • 'K': Preserves the memory order as much as possible.

Return Value

Returns a new one-dimensional array that contains all elements of the original array in the specified order. The returned array is a copy, meaning modifications do not affect the original array.


Examples

1. Flattening an Array Using Default Order (‘C’)

By default, flatten() collapses the array in row-major (C-style) order.

</>
Copy
import numpy as np

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

# Flattening the array using default order ('C' - row-major order)
flattened_arr = arr.flatten()

print("Original Array:\n", arr)
print("\nFlattened Array:", flattened_arr)

Output:

Original Array:
 [[1 2 3]
 [4 5 6]]

Flattened Array: [1 2 3 4 5 6]

The elements are read row-wise from the 2D array and converted into a 1D array.

2. Flattening in Column-Major (‘F’) Order

Using order='F' reads the elements column-wise (Fortran-style).

</>
Copy
import numpy as np

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

# Flattening the array in Fortran-style (column-major order)
flattened_arr_F = arr.flatten(order='F')

print("Original Array:\n", arr)
print("\nFlattened Array (Fortran order):", flattened_arr_F)

Output:

Original Array:
 [[1 2 3]
 [4 5 6]]

Flattened Array (Fortran order): [1 4 2 5 3 6]

Elements are read column-wise: first column (1, 4), second column (2, 5), and third column (3, 6).

3. Using order='A' (Automatic Order Selection)

If the array is Fortran-contiguous, 'A' follows Fortran order; otherwise, it defaults to row-major order.

</>
Copy
import numpy as np

# Creating an array with default memory layout
arr = np.array([[1, 2, 3], 
                [4, 5, 6]])

# Flattening with order 'A' (automatic)
flattened_arr_A = arr.flatten(order='A')

print("Original Array:\n", arr)
print("\nFlattened Array (Automatic order):", flattened_arr_A)

Output:

Original Array:
 [[1 2 3]
 [4 5 6]]

Flattened Array (Automatic order): [1 2 3 4 5 6]

Since the array is stored in row-major order by default, order='A' behaves like 'C' order.

4. Preserving Memory Layout with order='K'

The order='K' option tries to preserve the array’s memory order as much as possible.

</>
Copy
import numpy as np

# Creating an array and making it Fortran-contiguous
arr = np.array([[1, 2, 3], 
                [4, 5, 6]], order='F')

# Flattening while preserving memory layout
flattened_arr_K = arr.flatten(order='K')

print("Original Array:\n", arr)
print("\nFlattened Array (Preserved order):", flattened_arr_K)

Output:

Original Array:
 [[1 2 3]
 [4 5 6]]

Flattened Array (Preserved order): [1 4 2 5 3 6]

Since the original array is Fortran-contiguous, order='K' follows column-major order.