Converting a 2D array into a 1D array is called flattening. There are many approaches to solve the problem.
We will explore some of them in this tutorial.
Let's see an example.
Input
[[1, 2, 3], [4, 5], [6, 7, 8, 9, 10]]
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#1. Loops
The most common way to solve the problem is by using loops. I think most of you already got it. Let's see the steps to solve the problem using loops.
- Initialize the list of lists with dummy data and name it as data.
- Now, initialize an empty list called flat_list.
- Iterate over the data.
- Unpack all the elements from the current list.
- Add them to the flat_list using the list append method.
- Print the result.
See the code for the problem below.
# initializing the data and an empty list data = [[1, 2, 3], [4, 5], [6, 7, 8, 9, 10]] flat_list = [] # iterating over the data for item in data: # appending elements to the flat_list flat_list += item # printing the resultantn flat_list print(flat_list)
You can use another loop to add sub-list elements to flat_list instead of a concatenation operator. We can also use list comprehensions instead of loops.
Both do the same work. Let's see the next way to solve the problem.
#2. Itertools ā Chain
We will use a method called chain from itertools built-in module.
The method chain iterates over each sub-list and returns the elements until there are no sub-lists in it. It returns an iterable that we have to convert it into a list.
Let's see the steps involved in solving the problem.
- Initialize the list of lists with dummy data and name it as data.
- Get the flatten iterable using itertools.chain(*data).
- Conver the resultant iterable into a list.
- Print the flatten list.
You can go through the code in the below snippet.
# importing the module import itertools # initializing the data data = [[1, 2, 3], [4, 5], [6, 7, 8, 9, 10]] # flattening the list and storing the result flat_list = itertools.chain(*data) # converting iterable to list and printing print(list(flat_list))
#3. Flatten Multi-Level Lists
We have seen how to flatten a list of lists. The above methods that we have discussed to flatten the list won't work for multi-level lists. Let's see an example.
Input
[1, [2, 3, [4, 5]], 6, [[7], [8, 9]]]
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9]
As we don't know the depth of the lists before the program, we have to use recursion to solve the problem.
- Initialize the data as shown in the example and name it as data.
- Initialize an empty list called flat_list.
- Write a function called flatten_list.
- Iterate over the elements of the given list.
- If the element is a list then recursively call the same function again.
- If the element is not a list, then append the element to the flat_list.
- Invoke the function with data.
- The function will fill all the elements in the flat_list list.
- Print the flat_list to check the output.
Phew! a lot of steps to code. Don't worry. Converting the above statements into code won't take more than mins.
# initializing the data and empty list data = [1, [2, 3, [4, 5]], 6, [[7], [8, 9]]] flat_list = [] # function def flatten_list(data): # iterating over the data for element in data: # checking for list if type(element) == list: # calling the same function with current element as new argument flatten_list(element) else: flat_list.append(element) # flattening the given list flatten_list(data) # printing the flat_list print(flat_list)
Remember, we didn't convert the existing list. Instead, we have created a new list with the given list element.
Conclusion
Hope you have enjoyed the tutorial. There might be many other ways to flatten a list in Python but I felt the above are probably the easiest ones.
Happy Coding š