NumPy flatten()

The flatten() method flattens a NumPy array without changing its data.

Example

import numpy as np

# create a two-dimensional array
array1 = np.array([[0, 1], [2, 3]])

# flatten an array array2 = array1.flatten()
print(array2) # Output: [0 1 2 3]

Here, array1 is a two-dimensional array that is flattened to a one-dimensional array with all its elements intact.


flatten() Syntax

The syntax of flatten() is:

ndarray.flatten(order)

flatten() Argument

The flatten() method takes one argument:

  • order (optional) - specifies the order in which the array elements are flattened

flatten() Return Value

The flatten() method returns the flattened one-dimensional array.


Example 1: Flatten a Multidimensional Array

import numpy as np

# create a 3-D array
array1 = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])

# flatten the array array2 = array1.flatten()
print(array2)

Output

 [0 1 2 3 4 5 6 7]

Specify order to Flatten an Array

We can flatten the array elements in any order by passing the order argument.

The order can be:

  • 'C' - flattens the elements row-wise (in C-style order)
  • 'F' - flattens the elements column-wise (in Fortran-style order)
  • 'A' - tries to preserve the original array's order, otherwise defaults to C-order.
  • 'K' - flattens the elements in the order they occur in memory, uses C-order by default.
import numpy as np

array1 = np.array([[[0, 1], [2, 3]],
                   [[4, 5], [6, 7]]])

# flatten the array row-wise array2 = array1.flatten(order = 'C')
print("C:",array2)
# flatten the array column-wise array3 = array1.flatten( order = 'F')
print("F:",array3)
# flatten the array to original array's order array4 = array1.flatten(order = 'A')
print("A:",array2)
# flatten the array in the order they occur in memory array5 = array1.flatten(order = 'K')
print("K:",array3)

Output

C: [0 1 2 3 4 5 6 7]
F: [0 4 2 6 1 5 3 7]
A: [0 1 2 3 4 5 6 7]
K: [0 4 2 6 1 5 3 7]

Difference Between Flatten and Ravel

The key differences between flatten() and ravel() are

  • flatten() is an ndarray object method whereas ravel() is a library-level function. For example,
import numpy as np

array1 = np.array([[1, 2], [3, 4]])

# flatten is used as an array object array2 = array1.flatten() # ravel is used as a library function array3 = np.ravel(array1)
print(array2) print(array3)

Output

[1 2 3 4]
[1 2 3 4]
  • ravel() works with a list of arrays, but flatten() doesn't.
import numpy as np

# create a list of arrays
array1 = [np.array([1, 2]), np.array([3, 4])]

# use ravel with a list of arrays raveledList = np.ravel(array1)
print(raveledList)
# using flatten with a list of arrays # raises an error flattenedList = array1.flatten()
print(flattenedList)

Output

[1 2 3 4]
Traceback (most recent call last):
  File "<string>", line 13, in <module>
ERROR!
AttributeError: 'list' object has no attribute 'flatten'
  • flatten() always returns a copy of the original array whereas ravel() makes a copy only when necessary.

To learn more, visit NumPy ravel().

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community