# How to use Python flatten List in 10 different ways

In today’s article, we will be discussing different ways to flatten a list in python. We will be learning about 10 distinct ways to flatten multidimensional or nested lists into single-dimensional ones. Out of all ways that we will be going through, you can choose the process you like the best – easy and needful for your program.

Before we start with different ways to flatten lists in python, let me just brief you about what a list is and what do we mean by flattening a list.

## What is list in Python

In Python, a list is a mutable collection of any number of different things in sequence. All of the elements of the list are enclosed in square brackets[, separated by a comma]. The items may be of several data kinds; for example, a list may contain integer, float, and string entries. A nested list is one that has another list as one of its items.

A simple list in Python

``python_list = [1, 2, 3, 4, 5]``

A nested python list

``nested_python_list = [[1,2,3], [4, 5, 6], [7, 8, 9]]``

## Flattening a list in python

Flattening lists in Python is the process of reducing multidimensional lists to one-dimensional lists. It’s essentially a mechanism for combining all of a nested list’s sublists into a single unified list. The flattened list of the nested list python list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] is a good example. python list = [1, 2, 3, 4, 5, 6, 7, 8, 9] is given.

There are many techniques to flatten the list but In this article, we’ll go over the 10 different techniques to flatten a list in Python in detail:

1. Flatten List in Python Using Shallow Flattening
2. List Comprehension
3. Without Recursion
4. With Itertools
5. NumPy Ravel
6. Using NumPy Flatten
7. NumPy Reshape
8. Using NumPy flat
9. NumPy concatenate
10. Lambda Function
11. One-Line Command
12. 15. Sum function
13. Functools Reduce + Operator.concat
14. Functools Reduce + Operator.iconcat
15. Using Django Flatten
16. By using Pandas Flatten
17. Using Matplotlib Flatten
18. Using Unipath Flatten
19. ByuUsing Setuptools Flatten
20. Using NLTK Flatten
21. Using Generator
22. Flatten a Dictionary to List

### Flatten List in Python Using Shallow Flattening

``````List = [[0,1,2],[3,4,5]]
shallow_flatten_list = []
for nested_list in List:
for item in nested_list:
shallow_flatten_list.append(item)
print(shallow_flatten_list)
``````

Output:

By transforming [[0, 1,2], [3,4,5]] into [0, 1, 2, 3,4,5], this simple example flattens a list of lists. Shallow flattening is the approach used to flatten a list in the example above. It can only be used with a list of lists of equal depth.

### Flatten List in Python Using List Comprehension

``````List = [[0,1,2], [3,4,5]]
comp_list = [item for sublist in List for item in sublist]
print(comp_list)
``````

Output:

List comprehension is a technique for flattening lists with only one line of code. If we dissect the code, we’ll see that it’s essentially a nested for loop. ‘For a sub in l’ is the first loop, and ‘for an item in sub’ is the second.

### Flatten List in Python Using Without Recursion

``````def flatten_list_without_recursion(non_flatten_list):

List = []

while non_flatten_list:

empty = non_flatten_list.pop()

if type(empty) == list:

non_flatten_list.extend(empty)
else:

List.append(empty)

List.sort()

return List
l= [[0, 1], [[5]], [6, 7]]
flatten_list_without_recursion(l)
``````

Output:

We define a function named flatten without rec to flatten without recursion (). This function continues the while loop until all of the members in the nested list of variable depth are popped out. The control enters a while loop, which continues until the specified list is empty. It checks the type of item that popped out of the list as it enters the loop. If the item is a list, the loop must be repeated over it. If it isn’t, it will be put on the flat list.

### Flatten List in Python With Itertools

``````import itertools
List = [[1,2],[5,6],[7,9]]
list_using_itertools = list(itertools.chain(*List))
print(list_using_itertools)
``````

Output:

When you import itertools into your Python program, you gain access to its built-in function itertools.chain(), which merges various nested list lists into a single list. The itertools.chain() function takes as an argument a 2-D list that needs to be flattened.

### Using NumPy Ravel

The NumPy library has three built-in functions for flattening nested lists and multidimensional arrays. Numpy.ravel(), numpy.flatten(), and numpy.reshape() are the three functions (-1).

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

Output:

### Using NumPy Flatten

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

Output:

This method is one of the fastest ways to flatten a list, thanks to improved algorithms and features. The flatten() method does not affect the contents of an array that already exists. Instead, a new flat array is return.

### Using NumPy Reshape

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

Output:

The NumPy package is not install by default in Python. You must install this package from a pip in order to use its built-in flattening capabilities. The flattening of lists is performed by all three functions, and they all produce identical results. The only variation between them is their time complexities or speed. Every time the flatten method flattens the array, it produces a copy. As a result, while dealing with a bigger set of values, it takes longer.

### Using NumPy flat

``````import numpy as np
List = np.array([[1,2,3], [4,5,6], [7,8,9]])
print(list(List.flat))
``````

Output:

The flat attribute of a NumPy array delivers the flattened object of an n-dimensional array. This attribute iterates over all of the elements in the 1D reshaped array.

### Using NumPy concatenate

``````import numpy as np
l = np.array([[1,2,3], [4,5,6], [7,8,9]])
print(list(np.concatenate(l)))
``````

Output:

Numpy concatenate is a Python function that joins all of the array’s sub-arrays together. In Python, you can only flatten a 2d list using this function. Concatenation can be used in place of the extend() or + operators.

### Using Lambda Function

The simplest way to declare functions in a single line is to use lambda functions. You may execute a variety of operations on the parameter by being able to take and return it. Using list comprehension and lambda functions, you can flatten an array as well.

``````list_lambda = lambda x: [i for row in x for i in row]
List = [[1,2,3], [4,5,6], [7,8,9]]
result = list_lambda(List)
print(result)
``````

Output:

Python’s lambda tool allows you to create small functions using just one line of code. You may fatten an array by combining this with list comprehension, as seen in the examples above. To begin, we’ll create a lambda function named flatten. This function creates a new array with each member using layered list comprehension. After that, we passed the 2d array to flatten the array. The result of the function is a flattened array, as seen in the output.