Understanding nested list comprehension Understanding nested list comprehension python python

Understanding nested list comprehension


Indeed, you are correct. This is described in detail in the Expressions section in the Python Language Reference.

Note especially the order of nesting of several fors in a single list comprehension, which is always left-to-right:

>>> matrix = [[1, 2], [3, 4]]>>> [item for item in row for row in matrix] # oops!Traceback (most recent call last):  File "<pyshell#1>", line 1, in <module>    [item for item in row for row in matrix]NameError: name 'row' is not defined>>> [item for row in matrix for item in row] # nesting is in left-to-right order[1, 2, 3, 4]


The short answer is: yes, you are correct in your understanding.

There's only a catch: the way you normally use nested list comprehension in python code is to operate on multidimensional arrays.

A typical example is when you operate on matrices:

>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]>>> [[el - 1 for el in row] for row in matrix][[0, 1, 2], [3, 4, 5], [6, 7, 8]]

As you can see the "nesting" works by operating on each dimension of the matrix.

In the examples you provided, it seems that ySet [unfortunate name btw, as sets are one of the types provided with python] is just a generic counter, which makes a bit harder to follow what is going on under the hood.

As for your first example:

>>> rows = ([1, 2, 3], [10, 20, 30])>>> [(min([row[i] for row in rows]),max([row[i] for row in rows])) for i in range(len(rows[0]))][(1, 10), (2, 20), (3, 30)]

You might wish to look into the zip built-in function:

>>> zip(rows[0], rows[1])[(1, 10), (2, 20), (3, 30)]

or for maximum brevity and elegance:

>>> zip(*rows)[(1, 10), (2, 20), (3, 30)]

HTH!