What is the flattening of a list? How to make a flat list out of a list of lists? We will be discussing all this in a short while. So, stay with him as we go on this journey.

Flattening of a list generally signifies the methods of removing dimensions from lists, i.e. making it a one-dimensional list. And, how do we do this? We can flatten a list by making use of list comprehensions, itertools, or even nested loops.

Making flat list out of a list of lists

Python lists can generally be of many dimensions and even one-dimensional. When, there are a number of lists, one inside another this is termed as a “nested list”. But, we can convert this list into a normal 1-D list and this is termed as a “flat list”.

So, let’s see how do we flatten lists in the sections below.

Flattening a list

There are three ways to flatten a Python list:

  • Making use of list comprehensions
  • Secondly, making use of nested for loops.
  • Or, by making use of itertools.chain() function

We will see all this through various the help of an ample number of examples, to make all these concepts very clear.

Using List Comprehensions

List comprehension makes use of the contents of an existing list to define the new list. It is very easy and uses compact syntaxes for creating a list from another list. It is thus, in turn, a very concise way to generate a new list by performing operations on every item in the existing list.

Let’s see one example :

num = [4, 5, 6]
new = [num * 3 for number in num]

The above list comprehension multiplies every number in the list by 3. Now, that we have gone through one example; let’s take another example to make this method distinctly clear.

items = [
	["Potato", "Beetroot", "Onion", "Spinach"],
	["Bread", "Egg"],
	["Cheese", "Turkey", "Bacon"]
]

In the nested list named “items”, the first list contains vegetable items for making sandwiches. The next list contains some other items and finally, the third list contains meat items.

Now, let’s see how to combine all these three lists in a single list of items.

items = [item for sublist in items for item in sublist]
print(items)

The above list comprehension iterates each item in the list “items”. The items are added one by one in a new list and are then printed.

The output of the above list comprehension :

["Potato", "Beetroot", "Onion", "Spinach","Bread", "Egg", "Cheese", "Turkey", "Bacon"]

See !! We have successfully converted the nested lists (three-list) into a single 1-D list. Now, that we have converted the above list and used the list comprehension method; let’s now make use of nested for loops for the same task.

Using Nested For Loop

Nested for loop is simply a loop inside another loop. So, let’s see how we make these lists a 1-D list. We see the application of nested for loop to make this possible.

new = []
for sublist in items:
	for item in sublist:
		new.append(item)

print(new)

Let’s see the output of the above code :

["Potato", "Beetroot", "Onion", "Spinach","Bread", "Egg", "Cheese", "Turkey", "Bacon"]

Using Itertools

Itertools is a Python library which has ample methods to iterate over objects and perform easy manipulations. We will be using the chain() method of itertools to accept a list of lists and return a flat list.

# First we will import the itertools library by using the import command
import itertools

# Now, we define the list of lists and then make use of the chain() function 
items = [
	["Potato", "Beetroot", "Onion", "Spinach"],
	["Bread", "Egg"],
	["Cheese", "Turkey", "Bacon"]
]
new = itertools.chain(*items)  # we use the * symbol to unpack the items in the list.

#Now we print the flattened list
print(list(items))

The output of the above program is a below :

["Potato", "Beetroot", "Onion", "Spinach","Bread", "Egg", "Cheese", "Turkey", "Bacon"]

Thus, we see that itertools is a very great method which we can make use of to flatten lists. This is a more advance approach.

CONCLUSION

We have seen that we can flatten lists in Python using list comprehensions, nested for loops and itertools library. The method of list comprehensions is the most suitable method or the most favoured method because it is the easiest and the simplest of the lot.

Because, though nested loops are effective too, but, they need more lines of code while itertools library can be a bit difficult for beginners.