Avoid redundant list comprehension

all and any can take a generator, so constructing a list first may be unnecessary.

A generator returns a single item in the sequence ad hoc, on each iteration, while a list first loads the full list to memory.

any stops the loops as soon as a truthy is encountered and all stops the loop as soon as a falsey is encountered, so they lend themselves well to working with generators.

Caution is needed though: it's possible that a developer would want to avoid short-circuiting the loop: when the proper functioning of the code relies on the loop being completed. For example:

In that case the emails would stop being sent as soon as one message failed to send. Probably not what the developer intended to happen.

If our GitHub code review bot spots this issue in your pull request it gives this advice:

code-review-doctorbotsuggested changes just now
helpers.py
1
+
any([item for item in items])
Suggested changes
-
any([item for item in items])
+
any(item for item in items)
Commit suggestion

all and any can take a generator, so constructing a list first may be unnecessary.

Read more
Update helpers.py
We're your Python code review copilot. Get code improvements right in your pull request with our GitHub code review bot.

Configuring this check

Code Review Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code avoid-redundant-list-comprehension in your pyproject.toml file.

Read more about configuring Code Review Doctor.