## Introduction to NumPy ravel Python Function

Guide to NumPy ravel Python Function – The **numpy.ravel()** function returns a flattened array that is continuous (1D array with all the input-array elements and with the same type as it). Only a copy is created if it is required.

**Syntax:**

numpy.ravel(array, order = 'C')

**Parameters :**

**array :**[array_like]Input array.**order :**[C-contiguous, F-contiguous, A-contiguous; optional]- C-contiguous order in memory(the last index varies the fastest) C order means that operating row-rise on the array will be slightly quicker
- FORTRAN-contiguous order in memory (the first index varies the fastest). F order means that column-wise operations will be faster.
- A-contiguous ‘A’ means to read/write the elements in Fortran-like index order if, an array is Fortran contiguous in memory, C-like order otherwise

**Return**: Flattened array having the same type as the Input array and order as per choice.

## NumPy ravel Python Function: Code Examples

**Example 01: **Shows that array.ravel is equivalent to reshape(-1, order=order)

```
# Python Program illustrating
# numpy.ravel() method
import numpy as np
array = np.arange(15).reshape(5, 3)
print("Original array : \n", array)
# Output comes like [ 0 1 2 ..., 12 13 14]
# as it is a long output, so it is the way of
# showing output in Python
print("\nravel() : ", array.ravel())
# This shows array.ravel is equivalent to reshape(-1, order=order).
print("\nnumpy.ravel() == numpy.reshape(-1)")
print("Reshaping array : ", array.reshape(-1))
```

**Output:**

**Example 02:** Showing ordering manipulation

```
# Welcome to softhunt.net
# Python Program illustrating
# numpy.ravel() method
import numpy as np
array = np.arange(15).reshape(5, 3)
print("Original array : \n", array)
# Output comes like [ 0 1 2 ..., 12 13 14]
# as it is a long output, so it is the way of
# showing output in Python
# About :
print("\nAbout numpy.ravel() : ", array.ravel)
print("\nnumpy.ravel() : ", array.ravel())
# Maintaining both 'A' and 'F' order
print("\nMaintains A Order : ", array.ravel(order = 'A'))
# K-order preserving the ordering
# 'K' means that is neither 'A' nor 'F'
array2 = np.arange(12).reshape(2,3,2).swapaxes(1,2)
print("\narray2 \n", array2)
print("\nMaintains A Order : ", array2.ravel(order = 'K'))
```

**Output:**

## FAQs

### How do you ravel a list in Python?

There are three ways to flatten a Python list:

- Using a list comprehension.
- Through nested for loop.
- Using the itertools. chain() method.

### What is the difference between Ravel and flatten?

**flatten:**always returns a copy.**ravel:**When possible, ravel returns a view of the original array. This isn’t obvious in the written output, but if you change the array ravel returns, the items in the original array may change. This will never happen if you change the items in an array produced by flatten. Because no memory is duplicated, ravel is frequently quicker, but you must be extra careful when altering the array it returns.

## Conclusion

That’s all for this article if you have any confusion contact us through our website or email us at *[email protected]* or by using LinkedIn.

**Suggested Articles:**