I found this statement to work, when trying to sum up all positive elements of an array:
<code>sum(x for i, x in enumerate(arr) if x > 0)
</code>
<code>sum(x for i, x in enumerate(arr) if x > 0)
</code>
sum(x for i, x in enumerate(arr) if x > 0)
But, honestly, I do not understand why this is legal python. Checking the reference it is easy to find the for-statement for a loop:
https://docs.python.org/3/reference/compound_stmts.html#the-for-statement
The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object:
for_stmt ::= “for” target_list “in” starred_list “:” suite
[“else” “:” suite]
This cannot be the construct used above.
Question: How can I find the part of the reference that explains why this is legal python code?