.. _use-a-generator:

use-a-generator / R1729
=======================

**Message emitted:**

Use a generator instead '%s(%s)'

**Description:**

*Comprehension inside of 'any', 'all', 'max', 'min' or 'sum' is unnecessary. A generator would be sufficient and faster.*

**Problematic code:**

.. literalinclude:: /data/messages/u/use-a-generator/bad.py
   :language: python

**Correct code:**

.. literalinclude:: /data/messages/u/use-a-generator/good.py
   :language: python

**Additional details:**

By using a generator you can cut the execution tree and exit directly at the first element that is ``False`` for ``all`` or ``True`` for ``any`` instead of
calculating all the elements. Except in the worst possible case where you still need to evaluate everything (all values
are True for ``all`` or all values are false for ``any``) performance will be better.

**Related links:**

- `PEP 289 – Generator Expressions <https://peps.python.org/pep-0289/>`_
- `Benchmark and discussion during initial implementation <https://github.com/PyCQA/pylint/pull/3309#discussion_r576683109>`_

Created by the `refactoring <https://github.com/PyCQA/pylint/blob/main/pylint/checkers/refactoring/refactoring_checker.py>`__ checker.