[New-bugs-announce] [issue41591] Comprehensions documentation

Walid Taha report at bugs.python.org
Wed Aug 19 13:40:02 EDT 2020

New submission from Walid Taha <wtaha at fb.com>:

The documentation for list comprehensions contains the following phrase:

"As we saw in the previous section, the nested listcomp is evaluated in the context of the for that follows it, so this example is equivalent to:"

This should be corrected, as it currently contradicts what was said previously, which is that list comprehensions and the conditional they contain are scoped in the same order as they appear (rather than the reverse).

This issue can be found on this page: https://docs.python.org/3/tutorial/datastructures.html

It also seems to appear in the most recent version:

To confirm that the first (and not the second statement) is correct, you may consider the following code:

for x in range(0,3):
  for y in range (0,x+1):

l=[(x,y) for x in range (0,3) for y in range (0,x+1)]

Which run on 3.7.5 produces the following output

[(0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (2, 2)]
[(0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (2, 2)]

assignee: docs at python
components: Documentation
messages: 375665
nosy: docs at python, wtaha
priority: normal
severity: normal
status: open
title: Comprehensions documentation
versions: Python 3.8

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list