You might need to check out numpy.flatten and numpy.ravel, both return a 1-d array from an n-d array.

Furthermore, if you're not going to modify the returned 1-d array, I suggest you use numpy.ravel, since it doesn't make a copy of the array, but just return a view of the array, which is much faster than numpy.flatten.

>>>a = np.arange(10000).reshape((100,100))

>>>%timeit a.flatten()
100000 loops, best of 3: 4.02 µs per loop

>>>%timeit a.ravel()
1000000 loops, best of 3: 412 ns per loop

Also check out this post.

Answer from Alcott on Stack Overflow
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.ndarray.flatten.html
numpy.ndarray.flatten — NumPy v2.5.dev0 Manual
>>> import numpy as np >>> a = np.array([[1,2], [3,4]]) >>> a.flatten() array([1, 2, 3, 4]) >>> a.flatten('F') array([1, 3, 2, 4])
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › numpy-ndarray-flatten-function-python
Numpy ndarray.flatten() function in Python - GeeksforGeeks
July 12, 2025 - The flatten() function is used to convert a multi-dimensional NumPy array into a one-dimensional array. It creates a new copy of the data so that original array stays unchanged. If your array has rows and columns or even more dimensions, then ...
🌐
Python Tutorial
pythontutorial.net › home › python numpy › numpy flatten()
NumPy flatten() - Python Tutorial
August 11, 2022 - Use the numpy array flatten() method to return a copy of an array collapsed into one dimension.
🌐
Codecademy
codecademy.com › docs › python:numpy › ndarray › .flatten()
Python:NumPy | ndarray | .flatten() | Codecademy
April 10, 2025 - The .flatten() method converts a multi-dimensional NumPy array into a one-dimensional array. This method creates a copy of the original array with all elements arranged in a single dimension while preserving the values and their order.
🌐
Leocon
leocon.dev › blog › 2021 › 09 › how-to-flatten-a-python-list-array-and-which-one-should-you-use
How to flatten a python list/array and which one should you use | Leonidas Constantinou
September 5, 2021 - Overall, I would conclude that I will always use the chain_from_iterable when I am working with Python Lists. Make sure you never use the _sum and the list_comprehension since they are really inefficient. ... I would use the ravel() method if I want a view of the array but maybe modify it's values later on since ravel will create a copy automatically when it's necessary
🌐
Real Python
realpython.com › python-flatten-list
How to Flatten a List of Lists in Python – Real Python
December 22, 2024 - If you call .flatten() on matrix, then you get a one-dimensional array containing all the data. You’ve flattened the original multidimensional array into a flat, or one-dimensional, one. Cool! ... In this tutorial, you’ve learned how to flatten a list of lists in Python.
Find elsewhere
🌐
datagy
datagy.io › home › numpy › flatten an array with numpy flatten
Flatten an Array with NumPy flatten • datagy
December 30, 2022 - The NumPy flatten function allows you to turn a multi-dimensional array into a single-dimensional array. The function allows you to easily flatten arrays in different ways, including column-wise and row-wise.
🌐
w3resource
w3resource.com › numpy › manipulation › ndarray-flatten.php
NumPy: numpy.ndarray.flatten() function - w3resource
March 24, 2023 - In the above code a two-dimensional NumPy array 'y' is created with the array() function, containing the values [[2, 3], [4, 5]]. Then, the flatten() method is called on this array with the parameter 'F', which specifies column-wise flattening.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.flatten.html
numpy.ndarray.flatten — NumPy v2.4 Manual
>>> import numpy as np >>> a = np.array([[1,2], [3,4]]) >>> a.flatten() array([1, 2, 3, 4]) >>> a.flatten('F') array([1, 3, 2, 4])
🌐
Awkward-array
awkward-array.org › doc › main › user-guide › how-to-restructure-flatten.html
How to flatten arrays, especially for plotting — Awkward Array 2.9.0 documentation
To destructure an array for plotting, ... string, where N is an integer), ... There are two functions that are responsible for flattening arrays: ak.flatten() with axis=None; and ak.ravel(); but you don’t want to apply them without thinking, because structure is important to the ...
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ndarray.flatten.html
numpy.ndarray.flatten — NumPy v2.1 Manual
>>> import numpy as np >>> a = np.array([[1,2], [3,4]]) >>> a.flatten() array([1, 2, 3, 4]) >>> a.flatten('F') array([1, 3, 2, 4])
🌐
GitHub
gist.github.com › 6e5f86d253776f1d68da
Flatten an array of arrays with Python · GitHub
Flatten an array of arrays with Python. GitHub Gist: instantly share code, notes, and snippets.
🌐
Educative
educative.io › answers › what-is-the-numpyndarrayflatten-method-in-python
What is the numpy.ndarray.flatten() method in Python?
The ndarray.flatten() method in Python is used to return a copy of a given array in such a way that it is collapsed into one dimension.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-flatten-a-2d-numpy-array-into-1d-array
Python | Flatten a 2D Numpy Array into 1D Array - GeeksforGeeks
December 10, 2025 - Let's explore different ways to flatten a 2D NumPy array into a 1D array. ravel() tries to return a view of the original array (not a copy). It reads values row-wise and presents them as a 1D array, making it the most efficient method. Python · import numpy as np a = np.array([[1, 2, 3], [4, 5, 6]]) res = a.ravel() print(res) Output ·
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › array › flatten
Python Numpy array flatten() - Flatten Multidimensional Array | Vultr Docs
April 10, 2025 - This code creates a simple 2D array and uses flatten() to transform it into a 1D array, producing the output [1 2 3 4 5 6]. The flatten() method on a NumPy array is the easiest approach to flatten 2D arrays in Python.
Top answer
1 of 16
7504

A list of lists named xss can be flattened using a nested list comprehension:

flat_list = [
    x
    for xs in xss
    for x in xs
]

The above is equivalent to:

flat_list = []

for xs in xss:
    for x in xs:
        flat_list.append(x)

Here is the corresponding function:

def flatten(xss):
    return [x for xs in xss for x in xs]

This is the fastest method. As evidence, using the timeit module in the standard library, we see:

$ python -mtimeit -s'xss=[[1,2,3],[4,5,6],[7],[8,9]]*99' '[x for xs in xss for x in xs]'
10000 loops, best of 3: 143 usec per loop

$ python -mtimeit -s'xss=[[1,2,3],[4,5,6],[7],[8,9]]*99' 'sum(xss, [])'
1000 loops, best of 3: 969 usec per loop

$ python -mtimeit -s'xss=[[1,2,3],[4,5,6],[7],[8,9]]*99' 'reduce(lambda xs, ys: xs + ys, xss)'
1000 loops, best of 3: 1.1 msec per loop

Explanation: the methods based on + (including the implied use in sum) are, of necessity, O(L**2) when there are L sublists -- as the intermediate result list keeps getting longer, at each step a new intermediate result list object gets allocated, and all the items in the previous intermediate result must be copied over (as well as a few new ones added at the end). So, for simplicity and without actual loss of generality, say you have L sublists of M items each: the first M items are copied back and forth L-1 times, the second M items L-2 times, and so on; total number of copies is M times the sum of x for x from 1 to L excluded, i.e., M * (L**2)/2.

The list comprehension just generates one list, once, and copies each item over (from its original place of residence to the result list) also exactly once.

2 of 16
2463

You can use itertools.chain():

>>> import itertools
>>> list2d = [[1,2,3], [4,5,6], [7], [8,9]]
>>> merged = list(itertools.chain(*list2d))

Or you can use itertools.chain.from_iterable() which doesn't require unpacking the list with the * operator:

>>> import itertools
>>> list2d = [[1,2,3], [4,5,6], [7], [8,9]]
>>> merged = list(itertools.chain.from_iterable(list2d))

This approach is arguably more readable than [item for sublist in l for item in sublist] and appears to be faster too:

$ python3 -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99;import itertools' 'list(itertools.chain.from_iterable(l))'
20000 loops, best of 5: 10.8 usec per loop
$ python3 -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]'
10000 loops, best of 5: 21.7 usec per loop
$ python3 -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(l, [])'
1000 loops, best of 5: 258 usec per loop
$ python3 -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99;from functools import reduce' 'reduce(lambda x,y: x+y,l)'
1000 loops, best of 5: 292 usec per loop
$ python3 --version
Python 3.7.5rc1
🌐
Reddit
reddit.com › r/learnpython › python 3.5: flatten out array then reconstruct array
r/learnpython on Reddit: Python 3.5: Flatten out array then reconstruct array
August 3, 2016 -

I have a numpy array with initial dimensions of [605, 700, 3]. I then reshape the array such that the dimensions are [(605*700), 3] (basically a flat list of 3 item tuples). The array is flattened so that it can be fed into a variety of analysis functions. However, I ultimately need to reconstruct the array so that it is [605, 700, 3]. How would I go about doing this?

EDIT: I forgot to mention that I need the values to return to their starting indices when the array is reconstructed.

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-flatten-list-to-individual-elements
Flatten List to Individual Elements - Python - GeeksforGeeks
July 12, 2025 - For numerical lists, NumPy provides a built-in method for flattening arrays efficiently.
🌐
IncludeHelp
includehelp.com › python › whats-the-correct-and-efficient-way-to-flatten-numpy-array.aspx
Python - What's the correct and efficient way to flatten NumPy array?
December 26, 2023 - # Import numpy import numpy as np # Creating a numpy array arr = np.arange(100).reshape((10,10)) # Display Original array print("Original array:\n",arr,"\n\n") # Using arr.flatten res = arr.flatten() # Display result print("Flatten Result:\n",res,"\n\n") # Using arr.ravel res = arr.ravel() # Display result print("Ravel Result:\n",res) In this example, we have used the following Python basic topics that you should learn: