numpy
python
flatten
ravel
programming

What is the difference between flatten and ravel functions in numpy?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

flatten() and ravel() both turn a NumPy array into one dimension, but they differ in how they handle memory. The short answer is that flatten() always returns a copy, while ravel() returns a view whenever possible and only copies when necessary.

flatten() Always Copies

flatten() is an array method that returns a new one-dimensional array containing the same values.

python
1import numpy as np
2
3arr = np.array([[1, 2], [3, 4]])
4flat = arr.flatten()
5
6flat[0] = 99
7
8print(arr)
9print(flat)

Because flat is a copy, changing it does not affect arr. This makes flatten() predictable when you want an independent result.

That predictability costs memory. If the original array is large, flatten() allocates a separate block for the flattened data.

ravel() Prefers a View

ravel() tries to avoid copying. If NumPy can represent the flattened array as a view over the original memory, it does so.

python
1import numpy as np
2
3arr = np.array([[1, 2], [3, 4]])
4rav = arr.ravel()
5
6rav[0] = 99
7
8print(arr)
9print(rav)

In this case, changing rav also changes arr because both arrays share the same underlying data.

That is why ravel() is often faster and more memory-efficient. It reuses data when possible instead of duplicating it.

When ravel() Still Copies

The phrase "returns a view whenever possible" matters. Some arrays are not laid out contiguously in memory, especially after slicing or transposing. In those cases, ravel() may need to create a copy anyway.

python
1import numpy as np
2
3arr = np.array([[1, 2, 3], [4, 5, 6]])
4transposed = arr.T
5rav = transposed.ravel()
6
7rav[0] = 99
8
9print(transposed)
10print(rav)

Depending on layout, the raveled result may no longer share memory with the original transposed view. That is why you should not rely on ravel() always being a view.

If you need to check, NumPy provides np.shares_memory().

python
1import numpy as np
2
3arr = np.array([[1, 2], [3, 4]])
4rav = arr.ravel()
5flat = arr.flatten()
6
7print(np.shares_memory(arr, rav))
8print(np.shares_memory(arr, flat))

Order Matters Too

Both functions support an order argument that controls how elements are read out. The default is row-major order, written as 'C'.

python
1import numpy as np
2
3arr = np.array([[1, 2], [3, 4]])
4
5print(arr.flatten(order='C'))
6print(arr.flatten(order='F'))

Using 'F' reads the array in column-major order. This changes the output order, but it does not change the fundamental copy-versus-view distinction.

How This Compares to reshape(-1)

You will also see arr.reshape(-1). That often behaves similarly to ravel() because it returns a view when possible, but it is a reshape operation first and a flattening convenience second.

If your intent is specifically "give me a one-dimensional representation," ravel() expresses that intent more clearly. If your intent is general shape manipulation, reshape() may be the better tool.

Choosing Between Them

Use flatten() when you need a completely independent one-dimensional copy. Use ravel() when you want the cheapest flattened representation and can tolerate shared memory when available.

That is the real difference in practice. The returned values look similar, but the mutation behavior and memory cost are not the same.

Common Pitfalls

The biggest pitfall is assuming ravel() always returns a view. It often does, but not always.

Another issue is mutating the result of ravel() without realizing the original array may change too. That can create very confusing bugs in numeric code.

Developers also sometimes use flatten() everywhere out of habit. That is safe, but it may do unnecessary copying on large arrays.

Finally, do not forget the memory order argument when working with Fortran-style data or algorithms that expect column-major traversal.

Summary

  • 'flatten() always returns a copy.'
  • 'ravel() returns a view when possible and copies only when needed.'
  • Mutating a raveled array may mutate the original array too.
  • 'flatten() is safer when you need independence, while ravel() is cheaper when shared memory is acceptable.'
  • Both functions support order arguments such as 'C' and 'F'.

Course illustration
Course illustration

All Rights Reserved.