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.

```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.

```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 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:

```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. You can solve this problem 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:

`pip install iteration-utilities`
```from iteration_utilities import deepflatten

multi_depth_list = [[0,1], [], [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.

```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:

```if len(L) == 1:

if type(L) == list:

result = flatten(L)

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:

```def flatten(L):

if len(L) == 1:

if type(L) == list:

result = flatten(L)

else:

result = L

elif type(L) == list:

result = flatten(L) + flatten(L[1:])

else:

result = [L] + 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:

```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:

```from iteration_utilities import deepflatten

multi_depth_list = [[0,1],[[2,[3,[4,[5,]]]]],[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:

```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:

```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:

```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()

```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()

```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)

```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, you can use the built-in function flatten() from the flatten_json library.

You first need to install it using pip:

`pip install flatten_json`

Then you can use this function in our code:

```from flatten_json import flatten

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:

```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: You can achieve the same operation using list comprehension too:

```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:

```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:

```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, ]]]]], [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:

```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:

```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:

```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.