From ND to 1D arrays
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Converting multidimensional (ND) arrays to one-dimensional (1D) arrays — called flattening or raveling — is one of the most common array operations in scientific computing. Machine learning models typically expect 1D feature vectors, many C libraries require contiguous 1D buffers, and serialization formats work with flat sequences. NumPy provides several methods to flatten arrays, each with different memory and performance characteristics.
Method 1: ndarray.flatten()
flatten() always returns a copy of the data as a contiguous 1D array:
Row-major vs Column-major Order
Method 2: ndarray.ravel()
ravel() returns a 1D view when possible, avoiding a copy:
ravel() is faster and more memory-efficient than flatten() because it avoids copying data when the array is already contiguous. Use flatten() when you need an independent copy.
Method 3: ndarray.reshape(-1)
reshape(-1) reshapes to 1D, returning a view when possible (same behavior as ravel()):
reshape(-1) is commonly used in machine learning pipelines where you chain multiple reshape operations:
Method 4: np.concatenate or np.hstack
For flattening a list of arrays into one 1D array:
Higher-Dimensional Arrays
All methods work on arrays of any dimensionality:
Comparison of Methods
| Method | Returns Copy | Speed | When to Use |
flatten() | Always | Slower | Need independent copy |
ravel() | Only if needed | Fastest | Read-only or safe mutation |
reshape(-1) | Only if needed | Fast | Chaining reshape operations |
np.concatenate | Always | Moderate | Merging multiple arrays |
Flattening in Other Languages
Python Lists (No NumPy)
PyTorch
Common Pitfalls
- Unintended mutation with ravel(): Since
ravel()returns a view, modifying the result modifies the original array. Useflatten()if you need an independent copy. - Non-contiguous arrays: Transposed or sliced arrays may not be contiguous in memory.
ravel()andreshape(-1)will silently make a copy in these cases. Check witharr.flags['C_CONTIGUOUS']. - Order matters for ML: Most frameworks expect row-major ('C') order. If you flatten a Fortran-order array with the default 'C' order, the element sequence may be unexpected. Explicitly pass
order='C'ororder='F'. - Memory with large arrays:
flatten()on a 10 GB array allocates another 10 GB. Useravel()to avoid the copy, or process the array in chunks. - Nested Python lists vs NumPy:
np.array(nested_list).flatten()only works if the nested list is rectangular (all sublists have the same length). For ragged lists, useitertools.chain.from_iterable.
Summary
- Use
arr.ravel()for the fastest, memory-efficient flattening (returns a view when possible) - Use
arr.flatten()when you need a guaranteed independent copy - Use
arr.reshape(-1)when chaining with other reshape operations - The default order is row-major ('C') — elements are read across rows first
- For non-NumPy nested lists, use list comprehensions or
itertools.chain.from_iterable
Related reading
- FutureWarning arrays to stack must be passed as a sequence type such as list or tuple. Support for non-sequence iterables is deprecated
- Game on the tree, cutting branch
- Generate a large random planar graph
- Generate a random integer from 0 to N-1 which is not in the list
- Generate all combinations from multiple lists
- generate all partitions of a set
- Generate an integer that is not among four billion given ones
- Generate Non-Degenerate Point Set in 2D - C

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.