We will explore some of them in this tutorial. Let’s see an example. Input Output

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

#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 Output 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. 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 🙂

A Guide to Flatten List   List of Lists in Python - 19A Guide to Flatten List   List of Lists in Python - 22A Guide to Flatten List   List of Lists in Python - 80A Guide to Flatten List   List of Lists in Python - 8A Guide to Flatten List   List of Lists in Python - 92A Guide to Flatten List   List of Lists in Python - 65A Guide to Flatten List   List of Lists in Python - 71A Guide to Flatten List   List of Lists in Python - 99A Guide to Flatten List   List of Lists in Python - 5A Guide to Flatten List   List of Lists in Python - 29A Guide to Flatten List   List of Lists in Python - 68A Guide to Flatten List   List of Lists in Python - 74A Guide to Flatten List   List of Lists in Python - 61A Guide to Flatten List   List of Lists in Python - 36A Guide to Flatten List   List of Lists in Python - 30A Guide to Flatten List   List of Lists in Python - 97A Guide to Flatten List   List of Lists in Python - 72A Guide to Flatten List   List of Lists in Python - 90A Guide to Flatten List   List of Lists in Python - 92A Guide to Flatten List   List of Lists in Python - 94A Guide to Flatten List   List of Lists in Python - 57A Guide to Flatten List   List of Lists in Python - 47A Guide to Flatten List   List of Lists in Python - 91