# Python: numpy.flatten() – Function Tutorial with examples

In this article we will learn about a function flatten() and how we can use it to flatten a numpy array of any shape.

## numpy.ndarray.flatten()

In Python’s Numpy module, a numpy array has a member function to flatten its contents i.e. convert array of any shape to a 1D numpy array,

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

Parameters:

• order: The order in which items from numpy array will be used,
• ‘C’: Read items from array row wise i.e. using C-like index order.
• ‘F’: Read items from array column wise i.e. using Fortran-like index order.
• ‘A’: Read items from array based on memory order of items

It returns a copy of the input array but in flattened shape i.e. 1D array.

Let’s understand this with some practical examples,

## Flatten a matrix or a 2D array to a 1D array using ndarray.flatten()

First of all, import the numpy module,

`import numpy as np`

Suppose we have a 2D Numpy array,

```# Create a 2D Numpy array from list of list
arr_2d = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

print(arr_2d)```

Output:

```[[0 1 2]
[3 4 5]
[6 7 8]]```

Now we want to convert this 2D Numpy array to a flat array i.e. a 1D Numpy array. Let’s see how to do that using flatten() function,

```# Convert the 2D array to 1D array
flat_array = arr_2d.flatten()

print('Flattened 1D Numpy Array:')
print(flat_array)```

Output:

`[0 1 2 3 4 5 6 7 8]`

So, this is how we can use flatten() function to get a flattened 1D copy of a numpy array of any shape.

### ndarray.flatten() returns a copy of the input array

flatten() always returns a copy of the input array i.e. any changes done in the returned array will not modify the original array.
Let’s verify this with an example,

```# Create a 2D Numpy array from list of list
arr_2d = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

# Convert the 2D array to 1D array
flat_array = arr_2d.flatten()

flat_array[2] = 100

print('Flattened 1D Numpy Array:')
print(flat_array)

print('Original 2D Numpy Array')
print(arr_2d)
```

Output:

```Flattened 1D Numpy Array:
[  0   1 100   3   4   5   6   7   8]
Original 2D Numpy Array
[[0 1 2]
[3 4 5]
[6 7 8]]```

We created a 1D array from a 2D array using flatten() function and then modified the 3rd element in the 1D numpy array. But the changes in this 1D array did not affect the original 2D numpy array. This proves that the returned flattened array is just a copy of the input numpy array.

## Flatten a 2D Numpy array along different axis using flatten()

ndarray.flatten() accepts an optional parameter order. It can be ‘C’ or ‘F’ or ‘A’, but the default value is ‘C’.
It tells the order in which items from input numpy array will be used,

• ‘C’: Read items from array row wise i.e. using C-like index order.
• ‘F’: Read items from array column wise i.e. using Fortran-like index order.
• ‘A’: Read items from array based on memory order of items.

Let’s discuss them one by one with examples,

We have a 2D Numpy array,

```# Create a 2D Numpy array from list of list
arr_2d = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])```

#### Flatten 2D array row wise

If it doesn’t pass the order parameter in flatten() function then its default value will be ‘C’. It means elements from a 2D array will be read row by row,

```flat_array = arr_2d.flatten(order='C')

print('Flattened 1D Numpy Array:')
print(flat_array)```

Output:

```Flattened 1D Numpy Array:
[0 1 2 3 4 5 6 7 8]```

#### Flatten 2D array column wise

If pass ‘F’ as the order parameter in flatten() function then it means elements from a 2D array will be read column,

```flat_array = arr_2d.flatten(order='F')

print('Flattened 1D Numpy Array:')
print(flat_array)```

Output:

```Flattened 1D Numpy Array:
[0 3 6 1 4 7 2 5 8]```

#### Flatten 2D array based on memory layout

Let’s create a transpose view of the 2D numpy array,

```# Create a transpose view of array
trans_arr = arr_2d.T

print('Transpose view of array:')
print(trans_arr)
```

Output:

```Transpose view of array:
[[0 3 6]
[1 4 7]
[2 5 8]]```

Now flatten this transposed view ROW WISE,

```flat_array = trans_arr.flatten(order='C')

print(flat_array )```

Output:

`[0 3 6 1 4 7 2 5 8]`

As the order parameter was ‘C’, therefore it read the elements from view object row wise. But the original memory layout view was neglected and the current layout of the view object was used.

Now flatten this transposed view based on memory layout using argument ‘A’

```flat_array = trans_arr.flatten(order='A')

print('Flattened 1D Numpy Array:')
print(flat_array)
```

Output:

```Flattened 1D Numpy Array:
[0 1 2 3 4 5 6 7 8]```

Instead of considering the current layout in view, it used the memory layout of the original array object to read items row wise.

## Flatten a 3D array to 1D numpy array using ndarray.flatten()

```# Create a 3D Numpy array
arr = np.arange(12).reshape((2,3,2))

print('3D Numpy array:')
print(arr)
```

Output:

```3D Numpy array:
[[[ 0  1]
[ 2  3]
[ 4  5]]

[[ 6  7]
[ 8  9]
[10 11]]]```

Now let’s flatten this 3D numpy array,

```# Convert 3D array to 1D
flat_array = arr.flatten()

print('Flattened 1D Numpy Array:')
print(flat_array)
```

Output:

`[ 0  1  2  3  4  5  6  7  8  9 10 11]`

## Flatten a list of arrays using ndarray.flatten()

Let’s create a list of numpy arrays,

```# Create a list of numpy arrays
arr = np.arange(5)
list_of_arr = [arr] * 5

print('Iterate over the list of a numpy array')
for elem in list_of_arr:
print(elem)
```

Output:

```Iterate over the list of a numpy array
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]```

Now convert this list of numpy arrays to a flat 1D numpy array,

```# Convert a list of numpy arrays to a flat array
flat_array = np.array(list_of_arr).flatten()

print('Flattened 1D Numpy Array:')
print(flat_array)```

Output:

```Flattened 1D Numpy Array:
[0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4]```

Flatten a list of lists using ndarray.flatten()

Create a 2D numpy array from a list of list and then convert that to a flat 1D Numpy array,

```# Create a list of list
list_of_lists = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]

# Create a 2D numpy array from a list of list and flatten that array
flat_array = np.array(list_of_lists).flatten()

print('Flattened 1D Numpy Array:')
print(flat_array)

# Convert the array to list
print('Flat List:')
print(list(flat_array))
```

Output:

```Flattened 1D Numpy Array:
[1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]
Flat List:
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
```

So, this is how we can use flatten() function in numpy.

The complete example is as follows,

```import numpy as np

def main():

print('*** Flatten a matrix or a 2D array to a 1D array using ndarray.flatten() ***')
# Create a 2D Numpy array from list of list
arr_2d = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

print('2D Numpy Array')
print(arr_2d)

# Convert the 2D array to 1D array
flat_array = arr_2d.flatten()

print('Flattened 1D Numpy Array:')
print(flat_array)

print('*** ndarray.flatten() returns a copy of the input array ***')

# Create a 2D Numpy array from list of list
arr_2d = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

# Convert the 2D array to 1D array
flat_array = arr_2d.flatten()

flat_array[2] = 100

print('Flattened 1D Numpy Array:')
print(flat_array)

print('Original 2D Numpy Array')
print(arr_2d)

print('**** Flatten a 2D Numpy array along different axis using flatten() ****')

# Create a 2D Numpy array from list of list
arr_2d = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

print('** Flatten 2D array Row Wise **')

flat_array = arr_2d.flatten(order='C')

print('Flattened 1D Numpy Array:')
print(flat_array)

print('** Flatten 2D array Column Wise **')

flat_array = arr_2d.flatten(order='F')

print('Flattened 1D Numpy Array:')
print(flat_array)

print('** Flatten 2D array based on memory layout **')

# Create a transpose view of array
trans_arr = arr_2d.T

print('Transpose view of array:')
print(trans_arr)

print('flatten this transposed view ROW WISE')
flat_array = trans_arr.flatten(order='C')

print('Flattened 1D Numpy Array:')
print(flat_array)

print('Flatten this transposed view based on memory layout')
flat_array = trans_arr.flatten(order='A')

print('Flattened 1D Numpy Array:')
print(flat_array)

print('*** Flatten a 3D array to 1D numpy array using ndarray.flatten() ***')

# Create a 3D Numpy array
arr = np.arange(12).reshape((2,3,2))

print('3D Numpy array:')
print(arr)

# Convert 3D array to 1D
flat_array = arr.flatten()

print('Flattened 1D Numpy Array:')
print(flat_array)

print('*** Flatten a list of arrays using ndarray.flatten() ***')

# Create a list of numpy arrays
arr = np.arange(5)
list_of_arr = [arr] * 5

print('Iterate over the list of a numpy array')
for elem in list_of_arr:
print(elem)

# Convert a list of numpy arrays to a flat array
flat_array = np.array(list_of_arr).flatten()

print('Flattened 1D Numpy Array:')
print(flat_array)

print('Flatten a list of lists using ndarray.flatten()')

# Create a list of list
list_of_lists = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]

# Create a 2D numpy array from a list of list and flatten that array
flat_array = np.array(list_of_lists).flatten()

print('Flattened 1D Numpy Array:')
print(flat_array)

# Convert the array to list
print('Flat List:')
print(list(flat_array))

if __name__ == '__main__':
main()```

Output

```*** Flatten a matrix or a 2D array to a 1D array using ndarray.flatten() ***
2D Numpy Array
[[0 1 2]
[3 4 5]
[6 7 8]]
Flattened 1D Numpy Array:
[0 1 2 3 4 5 6 7 8]
*** ndarray.flatten() returns a copy of the input array ***
Flattened 1D Numpy Array:
[  0   1 100   3   4   5   6   7   8]
Original 2D Numpy Array
[[0 1 2]
[3 4 5]
[6 7 8]]
**** Flatten a 2D Numpy array along different axis using flatten() ****
** Flatten 2D array Row Wise **
Flattened 1D Numpy Array:
[0 1 2 3 4 5 6 7 8]
** Flatten 2D array Column Wise **
Flattened 1D Numpy Array:
[0 3 6 1 4 7 2 5 8]
** Flatten 2D array based on memory layout **
Transpose view of array:
[[0 3 6]
[1 4 7]
[2 5 8]]
flatten this transposed view ROW WISE
Flattened 1D Numpy Array:
[0 3 6 1 4 7 2 5 8]
Flatten this transposed view based on memory layout
Flattened 1D Numpy Array:
[0 1 2 3 4 5 6 7 8]
*** Flatten a 3D array to 1D numpy array using ndarray.flatten() ***
3D Numpy array:
[[[ 0  1]
[ 2  3]
[ 4  5]]

[[ 6  7]
[ 8  9]
[10 11]]]
Flattened 1D Numpy Array:
[ 0  1  2  3  4  5  6  7  8  9 10 11]
*** Flatten a list of arrays using ndarray.flatten() ***
Iterate over the list of a numpy array
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
Flattened 1D Numpy Array:
[0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4]
Flatten a list of lists using ndarray.flatten()
Flattened 1D Numpy Array:
[1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]
Flat List:
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
```

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top