This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mattlong
Recipients docs@python, mattlong
Date 2011-12-07.21:49:53
SpamBayes Score 1.15218e-06
Marked as misclassified No
Message-id <>
The description of nesting list comprehensions in section 5.1.5 of the main Python tutorial ( is misleading at best and arguably incorrect. Where it says "To avoid apprehension when nesting list comprehensions, read from right to left." This is incorrect and conflicts directly with the comment at the bottom of PEP 202 (, which says the last index varies fastest.

Take the following example:

matrix = [[1,2],[3,4],[5,6]]
my_list = []
for row in matrix:
  for number in row

The current documentation would suggest I do the following to achieve the same result with list comprehensions since the for statements should be read from right to left:

matrix = [[1,2],[3,4],[5,6]]
my_list = [number for number in row for row in matrix]

Running this code will result in an error. The correct form is:

matrix = [[1,2],[3,4],[5,6]]
[number for row in matrix for number in row]

Clearly the nesting order only matters when the inner loop depends on the outer loop as in my example above. There is no such dependency in the documentation's example, which is why it is does not result in an Error.
Date User Action Args
2011-12-07 21:49:54mattlongsetrecipients: + mattlong, docs@python
2011-12-07 21:49:54mattlongsetmessageid: <>
2011-12-07 21:49:53mattlonglinkissue13549 messages
2011-12-07 21:49:53mattlongcreate