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.

classification
Title: Comprehensions documentation
Type: Stage:
Components: Documentation Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, shubh07, veky, wtaha
Priority: normal Keywords:

Created on 2020-08-19 17:40 by wtaha, last changed 2022-04-11 14:59 by admin.

Messages (7)
msg375665 - (view) Author: Walid Taha (wtaha) Date: 2020-08-19 17:40
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:
https://docs.python.org/3.10/tutorial/datastructures.html

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

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

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

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)]
msg375712 - (view) Author: Shubham Upreti (shubh07) * Date: 2020-08-20 15:11
Is this open? Can i take this up?
msg375713 - (view) Author: Vedran Čačić (veky) * Date: 2020-08-20 15:15
This is not nested listcomp. Nested listcomp is a listcomp inside a listcomp. What you wrote is one listcomp, with two for-clauses. It does "desugar" into a nested loop, but that doesn't make it a nested listcomp.
msg375720 - (view) Author: Walid Taha (wtaha) Date: 2020-08-20 16:04
Thank you, and good point, Vedran. That takes care of the example that I gave. What remains is the source of confusion, namely, the reference to the "previous section". With a quick search I was not able to find what this was referring to (other than the one that you correctly pointed out is not nested). Can you point me to that reference?
msg375725 - (view) Author: Vedran Čačić (veky) * Date: 2020-08-20 18:12
It refers to the sentence

"""The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. """

The expression at the start of listcomp is the one that's evaluated in the context of clauses following it, no matter what kind of expression it is. If it is another listcomp, then you have a nested listcomp, but that's just a nonspecial case.
msg375726 - (view) Author: Walid Taha (wtaha) Date: 2020-08-20 18:27
That makes perfect sense now, and I see what threw me off. Basically, there were not enough cues for me to see that there was an extra pair of square brackets in the example that had nested listcomps, and as a result, I assumed that a nested listcomp simply meant one with multiple for clauses in it (which you clarified is not considered a nested listcomp).

If I may make a suggestion, the phrase "the nested listcomp is evaluated" would not have confused me if it simply said "the main part of the outer listcomp is evaluated". This can help in two ways. First, it avoids the possible confusion that the discussion in the previous section was about nested listcomp (which you rightly point out it is not). Second, adding the word "outer" gives an additional cue that we actually have two nested listcomps here.

Thank you very much for your quick response and help with this issue!
msg375728 - (view) Author: Vedran Čačić (veky) * Date: 2020-08-20 18:41
On the contrary, I think it would be much more clearer if it focused on the one that was evaluated in the context. So, _inner_ listcomp is... (instead of "nested listcomp is..."). "Main part of outer" is just beating around the bush. But in fact I'm not motivated enough (nor powerful enough) to actually change this on my own.
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85757
2020-08-20 18:41:01vekysetmessages: + msg375728
2020-08-20 18:27:43wtahasetmessages: + msg375726
2020-08-20 18:12:52vekysetmessages: + msg375725
2020-08-20 16:04:39wtahasetmessages: + msg375720
2020-08-20 15:15:51vekysetnosy: + veky
messages: + msg375713
2020-08-20 15:11:18shubh07setnosy: + shubh07
messages: + msg375712
2020-08-19 17:40:02wtahacreate