  # Understanding a nested list in Python

Loops | Python Methods and Functions | String Variables

Let`s take a look at some examples to understand what nested lists can do:

Example 1:

` I want to create a matrix which looks like below: matrix = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] `

The code below uses nested for loops for this task:

` `

` matrix = []   for i in range ( 5 ):   # Add an empty list inside the list < p> matrix.append ([])   for j in range ( 5 ): matrix [i] .append (j)   print (matrix) `

` ` Output:

` [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3 , 4], [0, 1, 2, 3, 4]] `

The same result can be achieved by using a nested list comprehension in only one line:

 ` # Understanding the nested list ` ` matrix ` ` = ` ` [[j ` ` for ` ` j ` ` in ` ` range ` ` (` ` 5 ` `)] ` ` for ` ` i ` ` in ` ` range ` ` (` ` 5 ` `)] `   ` print ` ` (matrix) `

Exit:

` [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] `

Explanation :

The syntax of the above program is shown below:

[expression for i in range (5)] - & gt; which means that execute this expression and append its output to the list until variable i iterates from 0 to 4.

For example: - [i for i in range (5)] - & gt; In this case, the output of the expression
is simply the variable i itself and hence we append its output to the list while i
iterates from 0 to 4.

Thus the output would be - & gt; [0, 1, 2, 3, 4]

But in our case, the expression itself is a list comprehension. Hence we need to first
solve the expression and then append its output to the list.

expression = [j for j in range (5)] - & gt; The output of this expression is same as the
example discussed above.

Hence expression = [0, 1, 2, 3, 4].

Now we just simply append this output until variable i iterates from 0 to 4 which would
be total 5 iterations. Hence the final output would just be a list of the output of the
above expression repeated 5 times.

Output: [[0, 1, 2, 3, 4], [0, 1 , 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]

Example 2:

` Suppose I want to flatten a given 2-D list: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Expected Output: flatten_matrix = [1, 2, 3, 4, 5, 6, 7, 8, 9] `

This can be done using nested loops as follows as follows:

 ` # 2-D List ` ` matrix ` ` = ` ` [[` ` 1 ` `, ` ` 2 ` `, ` ` 3 ` `] , [` ` 4 ` `, ` ` 5 ` ], [ ` 6 ` `, ` ` 7 ` `, ` ` 8 ` `, ` ` 9 ` `]] `   ` flatten_matrix ` ` = ` ` [] ` ` `  ` for ` ` sublist ` ` in matrix: `` for val in sublist:   flatten_matrix.append (val)    print (flatten_mat rix) `

Exit:

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

Again, this can be done using the nested list comprehension shown below:

` `

` # 2-D List matrix = [[ 1 , 2 , 3 ], [ 4 , 5 ], [ 6 , 7 , 8 , 9 ]]    # Understanding a nested list to align a given two-dimensional matrix flatten_matrix = [val for sublist in matrix for val in sublist]    print (flatten_matrix) `

` ` Exit:

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

Explanation :

In this case, we need to loop over each element in the given 2-D list and append it
to another list. For better understanding, we can divide the list comprehension into
three parts:

The first line suggests what we want to append to the list. The second line is the
outer loop and the third line is the inner loop.

`for sublist in matrix` returns the sublists inside the matrix one by one which would be:

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

`for val in sublist` returns all the values ​​inside the sublist.

Hence if sublist = [1, 2, 3], `for val in sublist` - & gt; gives 1, 2, 3 as output one by one.

For every such val, we get the output as val and we append it to the list.

Example 3:

Suppose I want to flatten a given 2-D list and only include those strings whose lengths are less than 6:

planets = [[`Mercury`, `Venus`, `Earth`], [`Mars`, `Jupiter`, `Saturn`], [`Uranus`, `Neptune`, `Pluto`]]

Expected Output: flatten_planets = [`Venus`, `Earth`, `Mars`, `Pluto`]

This can be done using an if condition inside a nested for loop, which is shown below:

` `

` # 2-D List of planets planets = [[ `Mercury` , `Venus` , ` Ea rth` ], [ `Mars` , `Jupiter` , ` Saturn` ], [ `Uranus` , ` Neptune` , `Pluto` ]]   flatten_planets = []    for sublist in planets: for planet in sublist:    if len (planet) & lt;  6 : flatten_planets.append (planet)   print (flatten_planets) `

` ` Exit :

` flatten_matrix = [val for sublist in matrix for val in sublist] `

This can also be done using the nested list comprehension that was shown below:

 ` # 2-D Planets list ` ` planets ` ` = ` ` [[` ` `Mercury` ` ` , ` ` `Venus` ` `, ` `` Earth` ` `], [` `Mars` `, ` `` Jupiter` ` ` , ` ` `Saturn` ` `], [` `` Uranus` ` `, ` ` `Neptune` ` `, ` `` Pluto` ` `]] `   ` # Understanding a nested list with an if condition ` ` flatten_planets ` ` = ` ` [planet ` ` for ` ` sublist ` ` in ` ` planets ` ` for ` ` planet ` ` in sublist if len (planet ) & lt;  6 ] ``   print (flatten_planets) `

Exit:

` [`Venus`,` Earth`, `Mars`,` Pluto`] `

Explanation :

This example is quite similar to the previous example but in this example, we just
need an extra if condition to check if the length of a particular planet is less than
6 or not.

This can be divided into 4 parts as follows: