Numpy ndarray.flatten() function in Python
Last Updated :
18 Apr, 2025
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)
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)
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)
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)
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
Numpy MaskedArray.flatten() function | Python numpy.MaskedArray.flatten() function is used to return a copy of the input masked array collapsed into one dimension. Syntax : numpy.ma.flatten(order='C') Parameters: order : [âCâ, âFâ, âAâ, âKâ, optional] Whether to flatten in C (row-major), Fortran (column-major) order, or preserve the C/Fortran o
2 min read
Numpy ndarray.dot() function | Python The numpy.ndarray.dot() function computes the dot product of two arrays. It is widely used in linear algebra, machine learning and deep learning for operations like matrix multiplication and vector projections.Example:Pythonimport numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) result =
2 min read
numpy.ndarray.flat() in Python The numpy.ndarray.flat() function is used as a 1_D iterator over N-dimensional arrays. It is not a subclass of, Pythonâs built-in iterator object, otherwise it a numpy.flatiter instance. Syntax : numpy.ndarray.flat() Parameters : index : [tuple(int)] index of the values to iterate Return :  1-D i
3 min read
Numpy MaskedArray.astype() function | Python In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays.Masked arrays are arra
2 min read
Numpy MaskedArray.all() function | Python In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays. Masked arrays are arr
3 min read
Numpy MaskedArray.argsort() function | Python In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays.Masked arrays are arra
3 min read
numpy.ndarray.fill() in Python numpy.ndarray.fill() method is used to fill the numpy array with a scalar value. If we have to initialize a numpy array with an identical value then we use numpy.ndarray.fill(). Suppose we have to create a NumPy array a of length n, each element of which is v. Then we use this function as a.fill(v).
2 min read
numpy.ndarray.resize() function - Python numpy.ndarray.resize() function change shape and size of array in-place. Syntax : numpy.ndarray.resize(new_shape, refcheck = True) Parameters : new_shape :[tuple of ints, or n ints] Shape of resized array. refcheck :[bool, optional] If False, reference count will not be checked. Default is True. Ret
1 min read
numpy.ma.filled() function - Python numpy.ma.filled() function return input as an array with masked data replaced by a fill value. If arr is not a MaskedArray, arr itself is returned. If arr is a MaskedArray and fill_value is None, fill_value is set to arr.fill_value. Syntax : numpy.ma.filled(arr, fill_value = None) Parameters : arr :
1 min read
numpy.ma.append() function | Python numpy.ma.append() function append the values to the end of an array. Syntax : numpy.ma.append(arr1, arr2, axis = None) Parameters : arr1 : [array_like] Values are appended to a copy of this array. arr2 : [array_like] Values are appended to a copy of this array. If axis is not specified, arr2 can be
2 min read