Open In App

Numpy ndarray.flatten() function in Python

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The flatten() function is used to convert a multi-dimensional NumPy array into a one-dimensional array. It creates a new copy of the data so that original array stays unchanged. If your array has rows and columns or even more dimensions, then flatten() line up every single value into a straight list, one after another. Example:

Python
import numpy as np

a = np.array([[5, 6], [7, 8]])
res = a.flatten()
print(res)

Output
[5 6 7 8]

Explanation: This code creates a 2D NumPy array and uses flatten() to convert it into a 1D array in row-major order. It returns a new array [5, 6, 7, 8] without modifying the original.

Syntax of ndarray.flatten()

array.flatten(order='C')

Parameters: order ({‘C’, ‘F’, ‘A’, ‘K’}) Optional

  • 'C': Row-major (C-style) order.
  • 'F': Column-major (Fortran-style) order.
  • 'A': Column-major if the array is Fortran contiguous, otherwise row-major.
  • 'K': Flatten in the order the elements occur in memory.

Returns: A 1D copy of the input array, flattened according to the specified order.

Examples of ndarray.flatten()

Example 1: In this example, we will use the flatten() function with the 'F' parameter to flatten a 2D NumPy array in column-major order (also known as Fortran-style order). This means the function will read the elements column by column, instead of row by row.

Python
import numpy as np

a = np.array([[5, 6], [7, 8]])
res = a.flatten('F')
print(res)

Output
[5 7 6 8]

Explanation: flatten('F') convert it into a 1D array in column-major order (Fortran-style). The result is [5, 7, 6, 8], with elements listed column by column.

Example 2: In this example, we will demonstrate how to flatten two 2D NumPy arrays and then concatenate them into a single 1D array using the concatenate() function from NumPy.

Python
import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[7, 8, 9], [10, 11, 12]])

res = np.concatenate((a.flatten(), b.flatten()))
print(res)

Output
[ 1  2  3  4  5  6  7  8  9 10 11 12]

Explanation: This code flattens two 2D NumPy arrays a and b into 1D arrays and then concatenates them. The result is a single 1D array combining the flattened elements of both arrays.

Example 3: In this example, we demonstrate how to flatten a 2D NumPy array and then initialize a new 1D array of the same shape filled with zeros

Python
import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
res = np.zeros_like(a.flatten())
print(res)

Output
[0 0 0 0 0 0]

Explanation: This code creates a new array of the same shape, initialized with zeros using np.zeros_like(). The result is a 1D array of zeros matching the flattened shape of a.

Example 4: In this example, we create a 2D NumPy. Then we apply the max() method to find the maximum value among all the elements in the flattened array.

Python
import numpy as np

a = np.array([[4, 12, 8],[5, 9, 10],[7, 6, 11]])
res = a.flatten().max()
print(res)

Output
12

Explanation: This code flattens the 2D NumPy array a into a 1D array and then finds the maximum value using .max(). The result is 12, which is the largest value in the flattened array.


Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy