.. _consider-using-generator:

consider-using-generator / R1728
================================

**Message emitted:**

Consider using a generator instead '%s(%s)'

**Description:**

*If your container can be large using a generator will bring better performance.*

**Problematic code:**

.. literalinclude:: /data/messages/c/consider-using-generator/bad.py
   :language: python

**Correct code:**

.. literalinclude:: /data/messages/c/consider-using-generator/good.py
   :language: python

**Additional details:**

Removing ``[]`` inside calls that can use containers or generators should be considered
for performance reasons since a generator will have an upfront cost to pay. The
performance will be better if you are working with long lists or sets.

For ``max``, ``min`` and ``sum`` using a generator is also recommended by pep289.

**Related links:**

- `PEP 289 <https://peps.python.org/pep-0289/>`_
- `Benchmark and discussion for any/all/list/tuple <https://github.com/PyCQA/pylint/pull/3309#discussion_r576683109>`_
- `Benchmark and discussion for sum/max/min <https://github.com/PyCQA/pylint/pull/6595#issuecomment-1125704244>`_

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