# 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 Convert Bytes to String

In this tutorial, we will take a look at how to convert bytes to string in Python.  We can convert bytes to string using the below methods  Using decode() method… ## Python String isalpha()

Python string isalpha() method is mainly used to check if the string is the alphabet or not. The isalpha() method returns true if all the characters in the string are… ## TypeError: ‘module’ object is not callable

Python throws TypeError: ‘module’ object is not callable when you get confused between the class name and module name. There are several reasons why this might happen while coding. Let’s… ## Python String lower()

Table of Contents Hide lower() Syntax lower() Parameterslower() Return ValueExample 1: Convert a string to lowercaseExample 2: String with Alphanumeric charactersExample 3: How to check if two strings are same… ## Python String rsplit()

Table of Contents Hide SyntaxParametersReturn ValueExample 1: How split() method works in PythonExample 2: Python split() method with maxsplit and separator arguments The Python String rsplit() method is a built-in… ## Sort Dictionary by value in Python

Table of Contents Hide Python sorted() function to rescueSyntax: sorted(iterable, key, reverse)Sort a Dictionary by ValueExample 1: Basic Sorting in PythonExample 2 : Sorting different Data Types in PythonExample 3: Sort…