Nested list comprehension Python

5 months ago10 minutes read

Nested List Comprehension in Python: If-else, loop Examples

Open Image

We can take an example where we want to pair each element of two lists then the general solution will be like this:

list1 = [1,2] list2 = [4,5] combinedList = [] for i in list1: for j in list2: combinedList.append[tuple[[i,j]]] print[combinedList] #[[1, 4], [1, 5], [2, 4], [2, 5]]

But we can use list comprehensions to do this in one line.

list1 = [1,2] list2 = [4,5] combinedList = [[i,j] for i in list1 for j in list2] print[combinedList]

Output

[[1, 4], [1, 5], [2, 4], [2, 5]]

If we see the general form of list comprehensions then it will be:

[i,j,k for i in ITERABLE1 for j in ITERABLE2 for k in ITERABLE3 ......[and so on]]

If-else List Comprehension in Python

It is the most used type of list comprehensions in python where we can create a list from an iterable based on some condition.

It is most commonly used to for loop inside list comprehensions.

An example for if-else inside list comprehensions will be to find even and odd numbers in any list.

some_list = [1,4,7,12,19,22, 23, 26] new_list = ["{} is even".format[i] if[i%2==0] else "{} It is odd".format[i] for i in some_list] print[new_list]

Output

['1 It is odd', '4 is even', '7 It is odd', '12 is even', '19 It is odd', '22 is even', '23 It is odd', '26 is even']

Nested List Comprehensions

It is possible to use list comprehensions in the nested list of another list.

Example to transpose rows and columns of a matrix with the use of nested list comprehensions in Python.

someMatrix = [ [3, 5, 1, 6], [1, 0, 9, 5], [4, 14, 22, 17], ] transpose = [[row[i] for row in someMatrix] for i in range[len[someMatrix[0]]]] print[transpose] # [[3, 1, 4], [5, 0, 14], [1, 9, 22], [6, 5, 17]]

The logic of the above list comprehension is similar to the following code.

transpose = [] for i in range[4]: # The next 3 lines are for nested list compression transpose_row = [] for row in someMatrix: transpose_row.append[row[i]] transpose.append[transpose_row] print[transpose]

When to avoid the use of List comprehensions?

Feeling excited to use list comprehensions everywhere in your code? Wait one second.

Using too much List comprehension in python will make your code unreadable.

Yes, list comprehension is very useful to write more beautiful and pythonic code. It is always recommended to use list comprehension in Python but maintain the code readability.

If you use list comprehension for a very complex problem then it will be a nightmare for other developers to read your code.

Always remember one thing, what you know about your code others will take time to understand it. So as responsible developers we need to write code that others can read without a problem.

You can use list comprehensions freely in the code which is not for human reading like in programming exams [thats where I use it most].

Conclusion

It seems very cool to use list Comprehensions everywhere. But it has its own cost.

List comprehension makes the code hard to understand, it is always recommended to avoid list compression in complex cases.

As a responsible developer, we should comment the code properly where we use list compression because it will help others to easily understand the code.

You can freely use list compression where you dont expect humans to read the code or you want the code reader to focus on other parts of code other than that [compressed].

If you have any problem, suggestions and examples then feel free to share them in comments.

Join Our Youtube Channel

Subscribe
#Python
Load Comments

Video liên quan

Chủ Đề