# numpy.ndarray.flatten() function

The `numpy.ndarray.flatten()` function in the NumPy array library is used to flatten an array of elements in different orders.

The flattening of a NumPy n-dimensional array in python is a procedure to return a copy of an array that is collapsed to a one-dimensional NumPy array.

The flattening of the NumPy array can be done in 4 different orders, those are:

1. C-style or Row-Major style Order: the ordering of consecutive row elements residing next to each other.
2. F-Style (Fortran Style) or Column-Major Order: the ordering of consecutive column elements residing next to each other.
3. A-style: The ordering can be two types here.
• 1) column-major order if the NumPy array is Fortran Contiguous
• 2)Row-Major order if the NumPy array is Fortran Non-Contiguous.
4. K-Style Order: flatten in the order that elements occur in memory.

The `numpy.ndarray.flatten()` method will return a copy of the original array. Since the n-dimensional output array is a copy of the original NumPy array, changing the output array will not affect the value of the original NumPy array.

## Syntax

The syntax of `numpy.ndarray.flatten()` method is:

``````ndarray.flatten(order='C')
``````

## Parameters

The `numpy.ndarray.flatten()` method takes a single optional argument.

• order: {‘C’, ’F’, ‘A’, ’K’}: (Optional)– the type of order of flattening which can be any of the values as follows {‘C’, ’F’, ’A’, ’K’}. If no value is provided it takes the default value ‘C’ which is row-major style order.

## Return Value

The method `ndarray.flatten()` returns a copy of a 1-dimensional array with the same type of elements.

## Example 1: How to flatten an array using NumPy ndarray.flatten() method

The method `ndarray.flatten()` flattens the input array into a single-dimensional array. If the `order` argument is not provided or if the `order` value is passed as `None`, then the array returns would be default row-major ordering style also called C-Style ordering.

``````import numpy as np

# initialize the numpy n-D array
arr_in = np.array([[[2, 17], [45, 78]], [[88, 92], [60, 76]], [[76, 33], [20, 18]]])
print("The input array is:\n", arr_in)

print("The flattened numpy array in default order(C-style order) is:")

# flatten function will flatten input array to default ordering(C-)
print(arr_in.flatten())
``````

Output

``````The input array is:
[[[ 2 17]
[45 78]]

[[88 92]
[60 76]]

[[76 33]
[20 18]]]

The flattened numpy array in default order(C-style order) is:
[ 2 17 45 78 88 92 60 76 76 33 20 18]``````

## Example 2: Flattening an array using numpy.flatten() method with C-style ordering

If we pass `order='C'` as an argument for the method `ndarray.flatten()`, the method flattens the input array into a single-dimensional array and returns the array in row-major order style.

``````import numpy as np

# initialize the numpy n-D array
arr_in = np.array([[[2, 17], [45, 78]], [[88, 92], [60, 76]], [[76, 33], [20, 18]]])
print("The input array is:\n", arr_in)

print(
"The flattened numpy array in Row major order(C-style order) using order = 'C' is:"
)

# flatten row major ordering using order = 'C'
print(arr_in.flatten(order="C"))
``````

Output

``````The input array is:
[[[ 2 17]
[45 78]]

[[88 92]
[60 76]]

[[76 33]
[20 18]]]

The flattened numpy array in Row major order(C-style order) using order = 'C' is:
[ 2 17 45 78 88 92 60 76 76 33 20 18]``````

## Example 3: Flattening an array using numpy.flatten() method with Fortran-style ordering

If we pass `order='F'` as an argument for the method `ndarray.flatten()`, the method flattens the input array into a single-dimensional array and returns the array in column-major order style also called as F-Style (Fortan Style).

``````import numpy as np

# initialize the numpy n-D array
arr_in = np.array([[[2, 17], [45, 78]], [[88, 92], [60, 76]], [[76, 33], [20, 18]]])
print("The input array is:\n", arr_in)

print(
"The flattened numpy array using fortran order(Column major order) using order = 'F' is:"
)

# flatten fortran ordering using order = 'F'
print(arr_in.flatten(order="F"))
``````

Output

``````The input array is:
[[[ 2 17]
[45 78]]

[[88 92]
[60 76]]

[[76 33]
[20 18]]]

The flattened numpy array using fortran order(Column major order) using order = 'F' is:
[ 2 88 76 45 60 20 17 92 33 78 76 18]``````

## Example 4: Flattening an array using numpy.flatten() method with A-style ordering

If we pass `order='A'` as an argument for the method `ndarray.flatten()`, the method flattens the input array into a single-dimensional array and returns the array in Fortran contiguous ordering in memory.

``````import numpy as np

# initialize the numpy n-D array
arr_in = np.array([[[2, 17], [45, 78]], [[88, 92], [60, 76]], [[76, 33], [20, 18]]])
print("The input array is:\n", arr_in)

print(
"The flattened numpy array in column-major order if array is Fortran contiguous in memory using order = 'A'is:"
)

# flattening numpy array in column-major order if array is Fortran contiguous in memory
print(arr_in.flatten(order="A"))
``````

Output

``````The input array is:
[[[ 2 17]
[45 78]]

[[88 92]
[60 76]]

[[76 33]
[20 18]]]

The flattened numpy array in column-major order if array is Fortran contiguous in memory using order = 'A'is:
[ 2 17 45 78 88 92 60 76 76 33 20 18]``````

## Example 5: Flattening an array using numpy.flatten() method with K-style ordering

If we pass `order='K'` as an argument for the method `ndarray.flatten()`, the method flattens the input array into a single-dimensional array and returns the 1-D array as elements occur in memory either in contiguous or non-contiguous.

``````import numpy as np

# initialize the numpy n-D array
arr_in = np.array([[[2, 17], [45, 78]], [[88, 92], [60, 76]], [[76, 33], [20, 18]]])
print("The input array is:\n", arr_in)

print("The array is flattened to order of elements occurs in memory is:")

# flattened to order of elements occurs in memory
print(arr_in.flatten(order="K"))
``````

Output

``````The input array is:
[[[ 2 17]
[45 78]]

[[88 92]
[60 76]]

[[76 33]
[20 18]]]

The array is flattened to order of elements occurs in memory is:
[ 2 17 45 78 88 92 60 76 76 33 20 18]
``````

## Conclusion

The flattening of a NumPy n-dimensional array in python is a procedure to return an array that is collapsed to a one-dimensional NumPy array. We use the `numpy.ndarray.flatten()` function to flatten an array of elements in different orders.

Reference: NumPy Library

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

##### You May Also Like ## Python String zfill()

Table of Contents Hide SyntaxParametersReturn ValueExample 1: How zfill() function works in PythonExample 2: zfill() method in case of sign prefix The Python String zfill() method is a built-in function… ## Python type()

Table of Contents Hide type() Syntaxtype() Parameterstype() Return ValueDifference between type() function and isinstance() functionExample of isinstance() method in PythonExample 1: Finding the type of a Python objectExample 2: type()… ## Python Max int | Maximum value of int in Python

In this tutorial, we will look at what’s Python Max int in different versions of Python. Python 3 has unlimited precision that means there is no explicitly defined max limit.… ## How to Copy a File in Python?

Table of Contents Hide Modules to copy a file in Python shutil module top copy a file in Pythoncopy() copy2() copyfile() copyfileobj()os module to copy a file in Pythonpopen() system() subprocess… ## Python Compare Strings: A Step-By-Step Guide

In Python the strings are compared with == and != operators. These operators compare if the two given strings are equal or not and return true or false based on… ## Python String encode()

Table of Contents Hide SyntaxParameterReturn ValueExample 1- Encode the string to Utf-8 EncodingExample 2- UnicodeEncodeError while encoding stringExample 3- Handling Encoding errors with error parameters Python String encode() method is…