# How to flatten a numpy array in python using flatten() function

In this article, you will learn how to flatten a numpy array in python using flatten() function.

Before going further, we have to learn the syntax and parameters for this function.

Syntax: ndarray.flatten(order=’C’)
This function return a copy of the array collapsed into one dimension.

Parameters:
order : {‘C’, ‘F’, ‘A’, ‘K’}, optional
‘C’ means to flatten in row-major (C-style) order. ‘F’ means to flatten in column-major (Fortran- style) order. ‘A’ means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. ‘K’ means to flatten a in the order the elements occur in memory. The default is ‘C’.

Returns:
y : ndarray
A copy of the input array, flattened to one dimension.

#Numpy python program to flattened array

` ` ``` import numpy as np a = np.arange(10) print("Original array : \n", a) a = np.arange(10).reshape(2, 5) print("printing 2 * 5 dimentional array",a) b= a.flatten() print("printing Flattern array using flatten() function",b) C= a.flatten('C') print("printing Flattern array using flatten() function with order -C",C) F= a.flatten('F') print("printing Flattern array using flatten() function with order -F ",F) Test= a.flatten('A') print("printing Flattern array using flatten() function with order -A ",Test) K= a.flatten('K') print("printing Flattern array using flatten() function with order -K ",K) ``` ` ` ` `

Output:

Original array :
[0 1 2 3 4 5 6 7 8 9]
printing 2 * 5 dimentional array [[0 1 2 3 4]
[5 6 7 8 9]]
printing Flattern array using flatten() function [0 1 2 3 4 5 6 7 8 9]
printing Flattern array using flatten() function with order -C [0 1 2 3 4 5 6 7 8 9]
printing Flattern array using flatten() function with order -F [0 5 1 6 2 7 3 8 4 9]
printing Flattern array using flatten() function with order -A [0 1 2 3 4 5 6 7 8 9]
printing Flattern array using flatten() function with order -K [0 1 2 3 4 5 6 7 8 9] 