# Differences between Flatten() and Ravel() Numpy Functions

• Difficulty Level : Medium
• Last Updated : 23 Oct, 2020

We have two similar kind of ways to convert a ndarray to 1D array : Flatten() and Ravel()

```import numpy as np
a = np.array( [ (1,7,3,4),(3,2,4,1) ] )
#OUTPUT:
print( a.flatten() )
# [ 1,7,3,4,3,2,4,1 ]
print ( a.ravel() )
# [ 1,7,3,4,3,2,4,1 ]
```

The question arises here, why are there two numpy functions to do the same task ?

Differences between Flatten() and Ravel()

a.ravel():
(i) Return only reference/view of original array
(ii) If you modify the array you would notice that the value of original array also changes.
(iii) Ravel is faster than flatten() as it does not occupy any memory.
(iv) Ravel is a library-level function.

a.flatten() :
(i) Return copy of original array
(ii) If you modify any value of this array value of original array is not affected.
(iii) Flatten() is comparatively slower than ravel() as it occupies memory.
(iv) Flatten is a method of an ndarray object.

Let us check out the difference by this code

 `# Python code to differentiate``# between flatten and ravel in numpy``import` `numpy as np`` ` `# Create a numpy array``a ``=` `np.array([(``1``,``2``,``3``,``4``),(``3``,``1``,``4``,``2``)])`` ` `# Let's print the array a``print` `(``"Original array:\n "``) ``print``(a)`` ` `# To check the dimension of array (dimension =2)``# ( and type is numpy.ndarray )``print` `(``"Dimension of array-> "` `, (a.ndim))`` ` ` ` `print``(``"\nOutput for RAVEL \n"``) ``# Convert nd array to 1D array``b ``=` `a.ravel()`` ` `# Ravel only passes a view of ``# original array to array 'b'``print``(b)``b[``0``]``=``1000``print``(b)`` ` `# Note here that value of original``# array 'a' at also a becomes 1000``print``(a)`` ` `# Just to check the dimension i.e. 1``# (and type is same numpy.ndarray )``print` `(``"Dimension of array->"` `,(b.ndim))`` ` `print``(``"\nOutput for FLATTEN \n"``) `` ` `# Convert nd array to 1D array``c ``=` `a.flatten()`` ` `# Flatten passes copy of``# original array to 'c'``print``(c)``c[``0``]``=``0``print``(c)`` ` `# Note that by changing``# value of c there is no``# affect on value of original``# array 'a'``print``(a)`` ` `print` `(``"Dimension of array-> "` `, (c.ndim))`

```OUTPUT:
Original array:

[[1 2 3 4]
[3 1 4 2]]
Dimension of array->  2

Output for RAVEL

[1 2 3 4 3 1 4 2]
[1000    2    3    4    3    1    4    2]
[[1000    2    3    4]
[   3    1    4    2]]
Dimension of array-> 1

Output for FLATTEN

[1000    2    3    4    3    1    4    2]
[0 2 3 4 3 1 4 2]
[[1000    2    3    4]
[   3    1    4    2]]
Dimension of array->  1```