# Python List Flattening : How to Convert a Nested List to Flat List – Best Techniques

- Link: https://techaid24.com/convert-nested-list-to-flat-list/
- Published: 2026-10-24T11:03:45+06:00
- Author: Anonymous Guest

In this article, we&#039;ll take a look at Show 1) Convert Nested List To Flat List In Python 1.1) Method 1: 1.2) Method 2: 1.3) Method 3: 2) Flatten List Using List Compression 3) Flatten List Using NumPy In this tutorial, we will go through several methods to convert a Nested list to flat list using Python. Lists are probably the most used data type in Python. Lists being mutable offer a lot of flexibility to perform a number of operations on the items of the list. &nbsp; A list contains items separated by commas and enclosed within square brackets [ ].  Lists in Python can also have items of different data types together in a single list. A nested list is nothing but a list containing several lists for example [1,2,[3],[4,[5,6]]. Often we need to convert these nested lists to flat list to perform regular list operations on the data. Fortunately, in Python, there are many ways to achieve this. &nbsp; Convert Nested List To Flat List In Python def flatten(li): return sum(([x] if not isinstance(x, list) else flatten(x) for x in li), []) print(flatten([1, 2, [3], [4, [5, 6]]])) 1234567 &nbsp;def flatten(li): &nbsp;&nbsp;&nbsp;&nbsp;return sum(([x] if not isinstance(x, list) else flatten(x) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for x in li), []) &nbsp;print(flatten([1, 2, [3], [4, [5, 6]]]))&nbsp; Output: [1, 2, 3, 4, 5, 6] Method 1: import functools import operator li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]] flat_list = functools.reduce(operator.concat, li) print(flat_list) 12345678 &nbsp;import functools import operator &nbsp;li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]] flat_list = functools.reduce(operator.concat, li) print(flat_list)&nbsp; Output: [14, 215, 383, 87, 298, 374, 2, 3, 4, 5, 6, 7] &nbsp; Method 2: from functools import reduce li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]] flat_list = reduce(lambda x, y: x+y, li) print(flat_list) 1234567 &nbsp;from functools import reduce &nbsp;li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]] flat_list = reduce(lambda x, y: x+y, li) print(flat_list)&nbsp; Output: [14, 215, 383, 87, 298, 374, 2, 3, 4, 5, 6, 7] &nbsp; Method 3: This is one of the most efficient ways to flatten a large list. from itertools import chain li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]] flat_list = list(chain.from_iterable(li)) print(flat_list) 1234567 &nbsp;from itertools import chain &nbsp;li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]] flat_list = list(chain.from_iterable(li)) print(flat_list)&nbsp; Output: [14, 215, 383, 87, 298, 374, 2, 3, 4, 5, 6, 7] &nbsp; Flatten List Using List Compression li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]] flat_list = [item for sublist in li for item in sublist] print(flat_list) 12345 &nbsp;li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]] flat_list = [item for sublist in li for item in sublist] print(flat_list)&nbsp; Output: [14, 215, 383, 87, 298, 374, 2, 3, 4, 5, 6, 7] &nbsp; Flatten List Using NumPy Note that, NumPy doesn’t come bundled with Python Installation you have to install it which can be done simply using Pip Installer. pip install numpy 123 &nbsp;pip install numpy&nbsp; Once the installation is done you can run the code below, import numpy li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]] flat_list = list(numpy.concatenate(li).flat) print(flat_list) 1234567 &nbsp;import numpy li = [[14], [215, 383, 87], [298], [374], [2, 3, 4, 5, 6, 7]] &nbsp;flat_list = list(numpy.concatenate(li).flat) print(flat_list)&nbsp; Output: [14, 215, 383, 87, 298, 374, 2, 3, 4, 5, 6, 7]
