Skip to content

Conversation

@Hello-world150
Copy link

@Hello-world150 Hello-world150 commented Aug 28, 2025

Copy link
Contributor

@sergey-miryanov sergey-miryanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks valid for me.

efimov-mikhail
efimov-mikhail previously approved these changes Oct 21, 2025
>>> [x for x in range(10) if x % 2 and x % 3]
[1, 5, 7]

Note the second :keyword:`!if` is replaced by :keyword:`!and`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Note the second :keyword:`!if` is replaced by :keyword:`!and`.
Note that the second :keyword:`!if` has been replaced with :keyword:`!and`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding 'that' would be correct if we add the note and patch, but I am considering whether to do so.

@pochmann3
Copy link
Contributor

Looks redundant/misleading to me. As I said in the issue, the conditions are not and-ed, the clauses are nested and the documentation already says and shows that.

Note how the order of the :keyword:`for` and :keyword:`if` statements is the
same in both these snippets.

For multiple :keyword:`!if` statements, like this::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those aren't if statements but if clauses.

@pochmann3
Copy link
Contributor

Looks redundant/misleading to me.

For example for a case of four clauses for-if-for-if, I can imagine the "and explanation" to confuse learners into thinking that those two ifs get and-ed somehow. Whereas thinking correctly about it, with nested statements as already documented, there's no issue.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this is needed. This is trivial, and it is the same as for nested ifs. There is no reason to use two consequent if clauses, you can simply use and. No need to document an anti-pattern.

@efimov-mikhail
Copy link
Member

No need to document an anti-pattern.

Framing this way I tend to change my mind. It'll be better to close this PR.

@sergey-miryanov sergey-miryanov dismissed their stale review October 22, 2025 08:55

Thanks to everyone for the reviews! It was also very helpful for my understanding. I will revoke my approval.

@efimov-mikhail efimov-mikhail dismissed their stale review October 22, 2025 09:54

There's no need in this change at all

@pochmann3
Copy link
Contributor

pochmann3 commented Oct 22, 2025

There is no reason to use two consequent if clauses

Not quite true, I have a reason and sometimes do that. When I split a "long" condition into multiple physical lines, I prefer one clause per line and having sub-conditions aligned vertically:

lst = [
    ...
    for x in ...
    if longCondition
    if anotherLongCondition
]

I wouldn't like an and there.

@serhiy-storchaka
Copy link
Member

Well, there may be stylistic reason. But it is not applicable for simple conditions like in the proposed example, and discussing stylistic nuances is out of scope of the language reference.

Copy link
Contributor

@willingc willingc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the submission.

Comment on lines 224 to 229
A list comprehension consists of brackets containing an expression followed
by a :keyword:`!for` clause, then zero or more :keyword:`!for` or :keyword:`!if`
clauses. The result will be a new list resulting from evaluating the expression
in the context of the :keyword:`!for` and :keyword:`!if` clauses which follow it.
For example, this listcomp combines the elements of two lists if they are not
equal::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A list comprehension consists of brackets containing an expression followed
by a :keyword:`!for` clause, which may be followed by zero or more :keyword:`!for` or :keyword:`!if`
clauses. The result will be a new list resulting from evaluating the expression
in the context of the :keyword:`!for` and :keyword:`!if` clauses which follow it.
For example, this listcomp combines the elements of two lists if they are not
equal::

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make this change as I think the current wording created the initial confusion in the issue.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your help. My English is not very good, please forgive me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

7 participants