Home » Python » 20+ Examples For Flattening Lists In Python # 20+ Examples For Flattening Lists In Python

Flattening lists means converting a multidimensional or nested list into a one-dimensional list. For example, the process of converting this [[1,2], [3,4]] list to [1,2,3,4] is called flattening.

The process of flattening is very easy as we’ll see. You will learn how to flatten different shapes of lists with different techniques. So, let’s jump in

## A list of lists

Let’s start with a simple example of converting [[0,1], [2,3]] into [0,1,2,3]. This type of flattening is called shallow flattening as it will only flatten lists of one level depth.

 0102030405060708091011 `l = [[``0``,``1``],[``2``,``3``]]` `flatten_list = []` `for` `subl in l:` `    ``for` `item in subl:` `        ``flatten_list.append(item)` `print(flatten_list)`

## Using list comprehension

List comprehension is a way to create lists in one line of code. Let’s see how we can use list comprehension for flattening the lists.

 12345 `l = [[``0``,``1``], [``2``,``3``]]` `flatten_list = [item ``for` `subl in l ``for` `item in subl]` `print(flatten_list)`

flatten_list = [item for subl in l for item in subl]

Let’s break this line of code.

The first loop is “for subl in l” and the second nested loop is “ for item in subl ”.

## Deep flattening

When we will try to flatten a list of varying depth like this [ [ 0, 1 ], [ [ 2 ] ][ 3, 4 ] ] list with shallow flattening, the output will be as follows:

 12345 `l = [ [``0``,``1``], [ [``2` `] ], [ ``3``, ``4` `] ]` `flatten_list = [item ``for` `subl in l ``for` `item in subl]` `print(flatten_list)`

But our goal is to convert [ [ 0, 1 ], [ [ 2 ] ], [ 3, 4 ] ] this list to this [ 0, 1, 2, 3, 4 ] list. This problem can be solved with deep flattening. In deep flattening, the process undergoes multiple levels of depths to create a flattened list.

There is a built-in function named deepflatten in the iteration_utilities library. You need to install this library using

 1 `pip install iteration-utilities`
 1234567 `from iteration_utilities ``import` `deepflatten` `multi_depth_list = [[``0``,``1``], [[``2``]], [``3``,``4``]]` `flatten_list = list(deepflatten(multi_depth_list))` `print(flatten_list)`

We have successfully achieved our target. Let’s take another example by changing the depth of the list.

 1234567 `from iteration_utilities ``import` `deepflatten` `multi_depth_list = [[``0``,``1``], [[``2``, [``3``, [``4``, [``5``, [``6` `]]]]]], [``7``,``8``]]` `flatten_list = list(deepflatten(multi_depth_list))` `print(flatten_list)`

## Using recursion

To flatten a list recursively, we will call the function inside itself to run until the end:

 123456789 `if` `len(L) == ``1``:` `        ``if` `type(L[``0``]) == list:` `                ``result = flatten(L[``0``])` `        ``else``:` `                ``result = L`

Check whether the list length is equal to 1. If true, then check whether the type of the first index of the list is a “list”.if true, then call the function that flattens the list else, store the number in the result.

The function will be like this:

 010203040506070809101112131415161718192021 `def flatten(L):``    ` `    ``if` `len(L) == ``1``:``        ` `            ``if` `type(L[``0``]) == list:``                ` `                    ``result = flatten(L[``0``])``                    ` `            ``else``:``                ` `                    ``result = L``                ` `    ``elif type(L[``0``]) == list:``        ` `            ``result = flatten(L[``0``]) + flatten(L[``1``:])``            ` `    ``else``:``        ` `            ``result = [L[``0``]] + flatten(L[``1``:])``        ` `    ``return` `result`

When we run this code against this [[0,1], , [3,4]] list, the results will be:

## Flatten without recursion

To flatten a list without recursion, we will use a while loop until we pop all the elements from it. Take a look at the code, you will have a better understanding:

 010203040506070809101112131415161718 `def flatten_without_rec(non_flat):``    ` `    ``flat = []``    ` `    ``while` `non_flat: #runs until the given list is empty.``        ` `            ``e = non_flat.pop()``            ` `            ``if` `type(e) == list: #checks the type of the poped item.``                ` `                    ``non_flat.extend(e) #``if` `list extend the item to given list.``            ``else``:``                ` `                    ``flat.append(e) #``if` `not list then add it to the flat list.``            ` `    ``flat.sort()``    ` `    ``return` `flat`

## Flatten nested lists

To flatten a nested list, you can use deep flattening. For deep flattening lists within lists, use the given below code:

 1234567 `from iteration_utilities ``import` `deepflatten` `multi_depth_list = [[``0``,``1``],[[``2``,[``3``,[``4``,[``5``,[``6``]]]]]],[``7``,``8``]]` `flatten_list = list(deepflatten(multi_depth_list))` `print(flatten_list)`

Also, you can use the recursive function as we did above.

## A list of tuples

Flattening a list of tuples of a single depth is the same as flattening lists within lists. For shallow flattening of tuples, use the following code:

 12345 `list_of_tuples = [(``1``,``2``),(``3``,``4``)]` `flatten_list = [item ``for` `subl in list_of_tuples ``for` `item in subl]` `print(flatten_list)`

The following will be the output:

For deep flattening, a list of tuples with varying depth (nested), you can use the code given below:

 1234567 `from iteration_utilities ``import` `deepflatten` `multi_depth_list = [(``0``,``1``),((``2``,(``3``,(``4``,(``5``,(``6``)))))),(``7``,``8``)]` `flatten_list = list(deepflatten(multi_depth_list))` `print(flatten_list)`

## Flatten 2d array

Let’s take a 2d array of 5×5 dimensions and convert it to a flattened list. Check the following code:

 010203040506070809101112131415 `from iteration_utilities ``import` `deepflatten` `rows = ``5` `cols = ``5` `array = [[i ``for` `i in range(cols)] ``for` `j in range(rows)]` `print(array)` `print(``"After applying flattening"``)` `flatten_list = list(deepflatten(array))` `print(flatten_list)`

The following will be the output of the above code:

## A list of NumPy arrays

There are three built-in functions defined in NumPy library that can convert the NumPy array into flattened lists.

numpy.ravel()

numpy.flatten()

numpy.reshape(-1)

### numpy.ravel()

 1234567 `import` `numpy as np` `lst = np.array([[``1``,``2``,``3``], [``4``,``5``,``6``], [``7``,``8``,``9``]])` `out = lst.ravel()` `print(out)`

The output of the code will be:

### numpy.flatten()

 1234567 `import` `numpy as np` `lst = np.array([[``1``,``2``,``3``], [``4``,``5``,``6``], [``7``,``8``,``9``]])` `out = lst.flatten()` `print(out)`

The output of the code will be:

### numpy.reshape(-1)

 1234567 `import` `numpy as np` `lst = np.array([[``1``,``2``,``3``], [``4``,``5``,``6``], [``7``,``8``,``9``]])` `out = lst.reshape(-``1``)` `print(out)`

The output of the code will be:

The difference between these three functions is speed. The flatten function returns a copy every time it flattens the array. So, if you have a large data set, don’t use the flatten function; it’s the slower one.

## Flatten JSON objects

For flattening JSON objects, there is a built-in function in the flatten_json library named flatten().

You first need to install it using pip:

 1 `pip install flatten_json`

Then you can use this function in our code:

 1234567 `from flatten_json ``import` `flatten` `sample_object = {``'FirstName'``:``'Ali'``, ``'Address'``:{``'City'``:``'Lahore'``,``'State'``:``'Punjab'``}}` `flat = flatten(sample_object)` `print(flat)`

The following will be the output of the code:

## Flatten a list of objects

You can flatten a list of objects using a built-in function available in the itertools library with function name itertools.chain.from_iterable() Let’s see how to use this function:

 01020304050607080910111213141516171819 `import` `itertools` `class` `numbers:` `    ``def __init__(self):``        ``pass`  `Object1, Object2, Object3 = [numbers() ``for` `_ in range(``3``)]` `List_of_objects = [Object1, Object2, Object3]` `Object1.myList = [``1``, ``2``, ``3``]` `Object2.myList = [``4``, ``5``, ``6``]` `Object3.myList = [``7``, ``8``, ``9``]` `print(list(itertools.chain.from_iterable(x.myList ``for` `x in List_of_objects)))`

The following will be the output of the above code:

The same operation can be achieved using list comprehension too:

 0102030405060708091011121314151617 `class` `numbers:` `    ``def __init__(self):``        ``pass`  `Object1, Object2, Object3 = [numbers() ``for` `_ in range(``3``)]` `List_of_objects = [Object1, Object2, Object3]` `Object1.myList = [``1``, ``2``, ``3``]` `Object2.myList = [``4``, ``5``, ``6``]` `Object3.myList = [``7``, ``8``, ``9``]` `[i ``for` `obj in List_of_objects ``for` `i in obj.myList]`

The following will be the output:

## Flatten a list of DataFrames

For flattening a list of DataFrames, the pandas library has a built-in function for flattening called df.concat() Let’s take a look at code:

 01020304050607080910111213 `import` `pandas as df` `dataframe1 = df.DataFrame({``'colum1'` `: [``1``, ``2``, ``3``, ``4``], ``'colum2'` `: [``4``., ``3``., ``2``., ``1``.]})` `dataframe2 = df.DataFrame({``'colum1'` `: [``5``, ``6``, ``7``, ``8``], ``'colum2'` `: [``9``., ``10``., ``11``., ``12``.]})` `dataframe3 = df.DataFrame({``'colum1'` `: [``15``, ``16``, ``17``, ``18``], ``'colum2'` `: [``19``., ``10``., ``11``., ``12``.]})` `list_of_dataframes = [dataframe1, dataframe2, dataframe3]` `flatten_df = df.concat(list_of_dataframes)` `print(flatten_df)`

The following will be the output.

## Flatten & remove duplicates

First, we will flatten our list, then we will remove the duplicates.

For flattening the list, we will use our own flatten_without_rec() function, and then we will remove the duplicates.

Let us have a look at the code:

 01020304050607080910111213141516171819202122232425262728 `def flatten_without_rec(non_flat):``    ` `    ``flat = []` `    ``while` `non_flat:  # runs until the given list is empty.` `        ``e = non_flat.pop()``        ` `        ``if` `type(e) == list:  # checks the type of popped item.``            ` `            ``non_flat.extend(e)  # ``if` `list extend the item in it to given list.``            ` `        ``else``:``            ``flat.append(e)  # ``if` `not list then add it to the flat list.``    ``flat.sort()``    ` `    ``return` `flat`  `nested_list = [[``0``, ``1``], [[``2``, [``3``, [``4``, [``5``, [``2``]]]]]], [``1``, ``2``]]` `flat_list = flatten_without_rec(nested_list)` `set_tuple = set(flat_list)  # converting the list into set to remove duplicates` `flat_list = list(set_tuple)  # converting the set back to list` `print(flat_list)`

The following will be the output:

## Flatten a dictionary into a list

You can flatten a dictionary to a list using a simple for loop:

Let us have a look at the code:

 01020304050607080910111213141516 `dic = {``      ``'alex'``: ``1``,``    ` `      ``'sharukh'``: ``2``,``    ` `      ``'flex'``: ``3``}``flat = []` `for` `k in dic:``    ` `    ``flat.append(k)``    ` `    ``flat.append(dic[k])``    ` `print(flat)`

The output of the following code will be like this:

You can also achieve the same using list comprehension:

 0102030405060708091011 `dic = {` `'alex'``: ``1``,` `'sharukh'``: ``2``,` `'flex'``: ``3` `}` `[item ``for` `k in dic ``for` `item in (k, dic[k])]`

The following will be the output of the code:

## Using reduce

The reduce() function is defined in the functools library. You first need to import reduce from the functools.

Let’s take a look at the code:

 12345 `from functools ``import` `reduce` `multi_depth_list = [[``1``,``2``,``3``],[``3``,``4``,``5``]]` `reduce(list.__add__, (list(items) ``for` `items in multi_depth_list))`

The output will be:

We flattened lists with different shapes & types in different ways. I hope you find the tutorial useful. Keep coming back.

Thank you.

 Published on Web Code Geeks with permission by Mokhtar Ebrahim, partner at our WCG program. See the original article here: 20+ Examples For Flattening Lists In PythonOpinions expressed by Web Code Geeks contributors are their own.  (0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....  