Without numpy ( ndarray.flatten ) one way would be using chain.from_iterable which is an alternate constructor for itertools.chain :

>>> list(chain.from_iterable([[1,2,3],[1,2],[1,4,5,6,7]]))
[1, 2, 3, 1, 2, 1, 4, 5, 6, 7]

Or as another yet Pythonic approach you can use a list comprehension :

[j for sub in [[1,2,3],[1,2],[1,4,5,6,7]] for j in sub]

Another functional approach very suitable for short lists could also be reduce in Python2 and functools.reduce in Python3 (don't use this for long lists):

In [4]: from functools import reduce # Python3

In [5]: reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]])
Out[5]: [1, 2, 3, 1, 2, 1, 4, 5, 6, 7]

To make it slightly faster you can use operator.add, which is built-in, instead of lambda:

In [6]: from operator import add

In [7]: reduce(add ,[[1,2,3],[1,2],[1,4,5,6,7]])
Out[7]: [1, 2, 3, 1, 2, 1, 4, 5, 6, 7]

In [8]: %timeit reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]])
789 ns ± 7.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [9]: %timeit reduce(add ,[[1,2,3],[1,2],[1,4,5,6,7]])
635 ns ± 4.38 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

benchmark:

:~$ python -m timeit "from itertools import chain;chain.from_iterable([[1,2,3],[1,2],[1,4,5,6,7]])"
1000000 loops, best of 3: 1.58 usec per loop
:~$ python -m timeit "reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]])"
1000000 loops, best of 3: 0.791 usec per loop
:~$ python -m timeit "[j for i in [[1,2,3],[1,2],[1,4,5,6,7]] for j in i]"
1000000 loops, best of 3: 0.784 usec per loop

A benchmark on @Will's answer that used sum (its fast for short list but not for long list) :

:~$ python -m timeit "sum([[1,2,3],[4,5,6],[7,8,9]], [])"
1000000 loops, best of 3: 0.575 usec per loop
:~$ python -m timeit "sum([range(100),range(100)], [])"
100000 loops, best of 3: 2.27 usec per loop
:~$ python -m timeit "reduce(lambda x,y :x+y ,[range(100),range(100)])"
100000 loops, best of 3: 2.1 usec per loop
Answer from Kasravnd on Stack Overflow
Top answer
1 of 3
128

Without numpy ( ndarray.flatten ) one way would be using chain.from_iterable which is an alternate constructor for itertools.chain :

>>> list(chain.from_iterable([[1,2,3],[1,2],[1,4,5,6,7]]))
[1, 2, 3, 1, 2, 1, 4, 5, 6, 7]

Or as another yet Pythonic approach you can use a list comprehension :

[j for sub in [[1,2,3],[1,2],[1,4,5,6,7]] for j in sub]

Another functional approach very suitable for short lists could also be reduce in Python2 and functools.reduce in Python3 (don't use this for long lists):

In [4]: from functools import reduce # Python3

In [5]: reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]])
Out[5]: [1, 2, 3, 1, 2, 1, 4, 5, 6, 7]

To make it slightly faster you can use operator.add, which is built-in, instead of lambda:

In [6]: from operator import add

In [7]: reduce(add ,[[1,2,3],[1,2],[1,4,5,6,7]])
Out[7]: [1, 2, 3, 1, 2, 1, 4, 5, 6, 7]

In [8]: %timeit reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]])
789 ns ± 7.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [9]: %timeit reduce(add ,[[1,2,3],[1,2],[1,4,5,6,7]])
635 ns ± 4.38 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

benchmark:

:~$ python -m timeit "from itertools import chain;chain.from_iterable([[1,2,3],[1,2],[1,4,5,6,7]])"
1000000 loops, best of 3: 1.58 usec per loop
:~$ python -m timeit "reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]])"
1000000 loops, best of 3: 0.791 usec per loop
:~$ python -m timeit "[j for i in [[1,2,3],[1,2],[1,4,5,6,7]] for j in i]"
1000000 loops, best of 3: 0.784 usec per loop

A benchmark on @Will's answer that used sum (its fast for short list but not for long list) :

:~$ python -m timeit "sum([[1,2,3],[4,5,6],[7,8,9]], [])"
1000000 loops, best of 3: 0.575 usec per loop
:~$ python -m timeit "sum([range(100),range(100)], [])"
100000 loops, best of 3: 2.27 usec per loop
:~$ python -m timeit "reduce(lambda x,y :x+y ,[range(100),range(100)])"
100000 loops, best of 3: 2.1 usec per loop
2 of 3
104

For just a list like this, my favourite neat little trick is just to use sum;

sum has an optional argument: sum(iterable [, start]), so you can do:

list_of_lists = [[1,2,3], [4,5,6], [7,8,9]]
print sum(list_of_lists, []) # [1,2,3,4,5,6,7,8,9]

this works because the + operator happens to be the concatenation operator for lists, and you've told it that the starting value is [] - an empty list.

but the documentaion for sum advises that you use itertools.chain instead, as it's much clearer.

Top answer
1 of 3
126

Without numpy ( ndarray.flatten ) one way would be using chain.from_iterable which is an alternate constructor for itertools.chain :

>>> list(chain.from_iterable([[1,2,3],[1,2],[1,4,5,6,7]]))
[1, 2, 3, 1, 2, 1, 4, 5, 6, 7]

Or as another yet Pythonic approach you can use a list comprehension :

[j for sub in [[1,2,3],[1,2],[1,4,5,6,7]] for j in sub]

Another functional approach very suitable for short lists could also be reduce in Python2 and functools.reduce in Python3 (don't use this for long lists):

In [4]: from functools import reduce # Python3

In [5]: reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]])
Out[5]: [1, 2, 3, 1, 2, 1, 4, 5, 6, 7]

To make it slightly faster you can use operator.add, which is built-in, instead of lambda:

In [6]: from operator import add

In [7]: reduce(add ,[[1,2,3],[1,2],[1,4,5,6,7]])
Out[7]: [1, 2, 3, 1, 2, 1, 4, 5, 6, 7]

In [8]: %timeit reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]])
789 ns ± 7.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [9]: %timeit reduce(add ,[[1,2,3],[1,2],[1,4,5,6,7]])
635 ns ± 4.38 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

benchmark:

:~$ python -m timeit "from itertools import chain;chain.from_iterable([[1,2,3],[1,2],[1,4,5,6,7]])"
1000000 loops, best of 3: 1.58 usec per loop
:~$ python -m timeit "reduce(lambda x,y :x+y ,[[1,2,3],[1,2],[1,4,5,6,7]])"
1000000 loops, best of 3: 0.791 usec per loop
:~$ python -m timeit "[j for i in [[1,2,3],[1,2],[1,4,5,6,7]] for j in i]"
1000000 loops, best of 3: 0.784 usec per loop

A benchmark on @Will's answer that used sum (its fast for short list but not for long list) :

:~$ python -m timeit "sum([[1,2,3],[4,5,6],[7,8,9]], [])"
1000000 loops, best of 3: 0.575 usec per loop
:~$ python -m timeit "sum([range(100),range(100)], [])"
100000 loops, best of 3: 2.27 usec per loop
:~$ python -m timeit "reduce(lambda x,y :x+y ,[range(100),range(100)])"
100000 loops, best of 3: 2.1 usec per loop
2 of 3
104

For just a list like this, my favourite neat little trick is just to use sum;

sum has an optional argument: sum(iterable [, start]), so you can do:

list_of_lists = [[1,2,3], [4,5,6], [7,8,9]]
print sum(list_of_lists, []) # [1,2,3,4,5,6,7,8,9]

this works because the + operator happens to be the concatenation operator for lists, and you've told it that the starting value is [] - an empty list.

but the documentaion for sum advises that you use itertools.chain instead, as it's much clearer.

Discussions

How can I reshape a 2D array into 1D in python? - Stack Overflow
Let me edit my question again. I know how flatten works but I am looking if it possible to remove the inside braces and just simple two outside braces just like in MATLAB and maintain the same shap... More on stackoverflow.com
🌐 stackoverflow.com
python - how can I change a two dimensional array to one dimensional - Stack Overflow
Check it's shape if you don't believe me. It's a 1d object dtype array of lists. ... @PaulRooney, your link is all about reshape, which flatten attempts to do. That's not applicable here. ... Can you try changing x to numpy array by using np.array(x) before calling concantenate? More on stackoverflow.com
🌐 stackoverflow.com
python - From ND to 1D arrays - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Turn scattered docs and code into trusted AI context. Explore the new Stack Internal platform More on stackoverflow.com
🌐 stackoverflow.com
How to convert a 2D index into a 1D index?
Next time you should just try it yourself. Your math is correct. More on reddit.com
🌐 r/csharp
9
4
March 4, 2024
🌐
Stack Overflow
stackoverflow.com › questions › 34713717 › combine-a-2d-array-into-1d-without-numpy
python - Combine a 2D array into 1D without numpy - Stack Overflow
The challenge is to convert this into a single list, on one line, importing only itertools (if it helps) ... Since most of the standard flattening idioms work fit on one line, I don't think we need a separate question. Plus, this is Python: we shouldn't care about cramming everything onto one line anyway.
🌐
CodersLegacy
coderslegacy.com › home › python › how to flatten 2d arrays or lists in python
How to flatten 2D Arrays or Lists in Python - CodersLegacy
February 16, 2023 - This is a very Python specific technique, called List Comprehension. Amongst it’s many uses, it can also be used to iterate over all the elements in the 2D array and add them into the 1D array.
🌐
YouTube
youtube.com › watch
How to Convert 2D Array to 1D Array in Python: Step-by-Step Guide - YouTube
Summary: Learn how to transform a two-dimensional array into a one-dimensional array in Python using list comprehension and nested loops. No NumPy required!-...
Published: September 10, 2024
Views: 203
Find elsewhere
🌐
Stack Abuse
stackabuse.com › python-how-to-flatten-list-of-lists
Python: How to Flatten a List of Lists
September 20, 2023 - This is a brute force approach to obtaining a flat list by picking every element from the list of lists and putting it in a 1D list. The code is intuitive as shown below and works for both regular and irregular lists of lists: def flatten_list(_2d_list): flat_list = [] # Iterate through the outer list for element in _2d_list: if type(element) is list: # If the element is of type list, iterate through the sublist for item in element: flat_list.append(item) else: flat_list.append(element) return flat_list nested_list = [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]] print('Original List', nested_list) print('Transformed Flat List', flatten_list(nested_list))
🌐
Quora
quora.com › How-do-I-convert-image-data-from-2D-array-to-1D-using-python
How to convert image data from 2D array to 1D using python - Quora
Answer (1 of 5): Assuming your image is a numpy/cv2/PIL image. numpy.ndarray.flatten - NumPy v1.11 Manual Otherwise just cast it.
🌐
Python Forum
python-forum.io › thread-16482.html
Transposing a Matrix WITHOUT numpy
March 1, 2019 - So our assignment is to ask for user input for two matrices and then multiply them and show the result then switch the result to a transpose. Since I did it in 1D instead of 2D, I have to transpose the original A and B rather than C. So how ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-flatten-a-2d-list
Python - Ways to Flatten a 2D list - GeeksforGeeks
July 11, 2025 - list() function is used to convert iterator into a list resulting in flattened list [1, 2, 3, 4, 5, 6, 7, 8]
🌐
GeeksforGeeks
geeksforgeeks.org › python-ways-to-flatten-a-2d-list
Python - Ways to Flatten a 2D list - GeeksforGeeks
January 31, 2025 - list() function is used to convert iterator into a list resulting in flattened list [1, 2, 3, 4, 5, 6, 7, 8] ... In Python, a list is a built-in dynamic sized array (automatically grows and shrinks).
🌐
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 ... presents them as a 1D array, making it the most efficient method. ... reshape(-1) converts any array into a 1D array....
🌐
Reddit
reddit.com › r/csharp › how to convert a 2d index into a 1d index?
r/csharp on Reddit: How to convert a 2D index into a 1D index?
March 4, 2024 -

Let's say we have a matrix with an x and a y axis

So let this simply be a grid.

(0, 1) (1, 1) (2, 1) (3, 1)

(0, 0) (1, 0) (2, 0) (3, 0)

This grid is stored in a 1D array [(0, 1), (0, 2), ... , (3, 1)]

If I have any grid coordinate (i, j), how do I convert from (i, j) to the index where it is stored in the 1D array?

So to get to (i, j), we travel up j steps, and then right i steps. As we travel up j steps, we cover the grid width j times

So I'm thinking, index = j * width + i

If we have a x, y grid, then (x, y) has the index y * width + x?

If we have a x, z grid, then (x, z) has the index z * width + x?

🌐
YouTube
youtube.com › watch
How to Convert 2D Array to 1D Array in Python: Step-by-Step Guide - YouTube
How to Convert 2D Array to 1D Array in Python: Step-by-Step GuideIn this tutorial, you will learn how to convert a 2D array to a 1D array in Python. We will ...
Published: January 27, 2025
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › array › flatten()
Python Numpy array flatten() - Flatten Multidimensional Array | Vultr Docs
April 10, 2025 - Explore practical examples of transforming complex arrays into simpler, single-dimensional arrays and understand the options available within the flatten() function to customize the flattening process.