In this article we will discuss different ways to convert a list of lists or nested lists to a single flat list.

Convert a list of lists to a flat list

Suppose we have a list of lists i.e.

This list contains 3 different lists of integers. We want to convert this list of lists in to a single flat list, that should contain only integers like,

There are different ways to do this,

Use list comprehension to convert a list of lists to a flat list

We will use list comprehension to iterate over a lists of list and then for each internal list again iterate over the individual elements in that list. Then add those elements to a new list i.e.

Output:

Although this is a single line solution but this kind of coding is not easy to maintain. So, let’s see some other options too,

Use list.extend() to convert a list of lists to a flat list

In python list data type provides a method to add all the contents of an iterable to the existing list,

It extends the existing list object by appending all the contents of given iterable. Let’s use this to convert list of lists to a flat list,

Output:

How did it worked ?

We created a new empty list. Then using for loop we iterated over the list of lists and then for each internal list appended its individual elements to our new flat list using list.extend().

Well we achieved our result, but there is an another way to do this,

Use list.append() to convert a list of lists to a flat list

In python list data type provides a method to add an item at the end of a list,

Let’s use this to convert list of lists to a flat list,

Output:

How did it worked ?

We created a new empty list. Then using for loop we iterated over the list of lists, then for each internal list again iterated over the individual elements in that list. Then add those individual elements to a new list using list.append()

All the above solution will work in case of list of lists. But what if we have nested list like list of numbers & lists. Also, internal lists might contain more lists. How to create a flat list from this kind of nested list ?

How to convert a nested list to a flat list ?

Suppose we have a nested list that contain some number, some lists and these internal lists also contain some numbers & lists i.e.

Now we want to convert this kind of nested list to a flat list like,

To do that we have created a recursive function,

It accepts a nested list as argument, then iterates over each element in that list. For each element it checks if its type is list or not.

  • If yes, then again calls the same function flattenNestedList() with this element (list) to get a flat list out of it. Then extends the main flat list with the returned flat list.
  • Whereas, if element is not a list then it adds that to the end of the list using list.append(),

Now let’s use this function to convert our nested list to a flat list i.e.

Output:

Complete example is as follows,

Output:

 

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,