{"id":13679099,"url":"https://github.com/adamchainz/flake8-comprehensions","last_synced_at":"2025-12-29T23:04:59.232Z","repository":{"id":6877613,"uuid":"55505248","full_name":"adamchainz/flake8-comprehensions","owner":"adamchainz","description":"❄️ A flake8 plugin to help you write better list/set/dict comprehensions.","archived":false,"fork":false,"pushed_at":"2025-04-11T15:12:57.000Z","size":747,"stargazers_count":468,"open_issues_count":4,"forks_count":23,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-11T16:27:57.260Z","etag":null,"topics":["flake8"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adamchainz.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"tidelift":"pypi/flake8-comprehensions","custom":["https://adamj.eu/books/"]}},"created_at":"2016-04-05T12:15:56.000Z","updated_at":"2025-04-11T15:13:00.000Z","dependencies_parsed_at":"2023-02-15T15:31:47.874Z","dependency_job_id":"19cf8c54-bb8e-4729-b6f2-45d23d5e710d","html_url":"https://github.com/adamchainz/flake8-comprehensions","commit_stats":{"total_commits":522,"total_committers":16,"mean_commits":32.625,"dds":"0.24329501915708818","last_synced_commit":"34dcd467c631bba7ae05d21c02d77d8ec24fe720"},"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamchainz%2Fflake8-comprehensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamchainz%2Fflake8-comprehensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamchainz%2Fflake8-comprehensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamchainz%2Fflake8-comprehensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adamchainz","download_url":"https://codeload.github.com/adamchainz/flake8-comprehensions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251540090,"owners_count":21605843,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flake8"],"created_at":"2024-08-02T13:01:01.949Z","updated_at":"2025-12-29T23:04:59.227Z","avatar_url":"https://github.com/adamchainz.png","language":"Python","funding_links":["https://tidelift.com/funding/github/pypi/flake8-comprehensions","https://adamj.eu/books/"],"categories":["Topics Index","Python","Clean code","Linters \u0026 Style Checkers"],"sub_categories":["Code Quality and Linting"],"readme":"=====================\nflake8-comprehensions\n=====================\n\n.. image:: https://img.shields.io/github/actions/workflow/status/adamchainz/flake8-comprehensions/main.yml.svg?branch=main\u0026style=for-the-badge\n   :target: https://github.com/adamchainz/flake8-comprehensions/actions?workflow=CI\n\n.. image:: https://img.shields.io/pypi/v/flake8-comprehensions.svg?style=for-the-badge\n   :target: https://pypi.org/project/flake8-comprehensions/\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge\n   :target: https://github.com/psf/black\n\n.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit\u0026logoColor=white\u0026style=for-the-badge\n   :target: https://github.com/pre-commit/pre-commit\n   :alt: pre-commit\n\nA `flake8 \u003chttps://flake8.readthedocs.io/en/latest/\u003e`_ plugin that helps you write better list/set/dict comprehensions.\n\n----\n\n**Linting a Django project?**\nCheck out my book `Boost Your Django DX \u003chttps://adamchainz.gumroad.com/l/byddx\u003e`__ which covers Flake8 and many other code quality tools.\n\n----\n\nRequirements\n============\n\nPython 3.10 to 3.14 supported.\n\nInstallation\n============\n\nFirst, install with ``pip``:\n\n.. code-block:: sh\n\n     python -m pip install flake8-comprehensions\n\nSecond, if you define Flake8’s ``select`` setting, add the ``C4`` prefix to it.\nOtherwise, the plugin should be active by default.\n\nRules\n=====\n\nC400-402: Unnecessary generator - rewrite as a ``\u003clist/set/dict\u003e`` comprehension.\n---------------------------------------------------------------------------------\n\nRules:\n\n* C400 Unnecessary generator - rewrite as a list comprehension.\n* C401 Unnecessary generator - rewrite as a set comprehension.\n* C402 Unnecessary generator - rewrite as a dict comprehension.\n\nIt's unnecessary to use ``list``, ``set``, or ``dict`` around a generator expression, since there are equivalent comprehensions for these types.\nFor example:\n\n* Rewrite ``list(f(x) for x in foo)`` as ``[f(x) for x in foo]``\n* Rewrite ``set(f(x) for x in foo)`` as ``{f(x) for x in foo}``\n* Rewrite ``dict((x, f(x)) for x in foo)`` as ``{x: f(x) for x in foo}``\n\nC403-404: Unnecessary list comprehension - rewrite as a ``\u003cset/dict\u003e`` comprehension.\n-------------------------------------------------------------------------------------\n\nRules:\n\n* C403 Unnecessary list comprehension - rewrite as a set comprehension.\n* C404 Unnecessary list comprehension - rewrite as a dict comprehension.\n\nIt's unnecessary to use a list comprehension inside a call to ``set`` or ``dict``, since there are equivalent comprehensions for these types.\nFor example:\n\n* Rewrite ``set([f(x) for x in foo])`` as ``{f(x) for x in foo}``\n* Rewrite ``dict([(x, f(x)) for x in foo])`` as ``{x: f(x) for x in foo}``\n\nC405-406: Unnecessary ``\u003clist/tuple\u003e`` literal - rewrite as a ``\u003cset/dict\u003e`` literal.\n-------------------------------------------------------------------------------------\n\n* C405 Unnecessary ``\u003clist/tuple\u003e`` literal - rewrite as a set literal.\n* C406 Unnecessary ``\u003clist/tuple\u003e`` literal - rewrite as a dict literal.\n\nIt's unnecessary to use a list or tuple literal within a call to ``set`` or ``dict``.\nFor example:\n\n* Rewrite ``set([1, 2])`` as ``{1, 2}``\n* Rewrite  ``set((1, 2))`` as ``{1, 2}``\n* Rewrite ``set([])`` as ``set()``\n* Rewrite ``dict([(1, 2)])`` as ``{1: 2}``\n* Rewrite ``dict(((1, 2),))`` as ``{1: 2}``\n* Rewrite ``dict([])`` as ``{}``\n\nC407: Unnecessary ``\u003cdict/list\u003e`` comprehension - ``\u003cbuiltin\u003e`` can take a generator\n------------------------------------------------------------------------------------\n\nThis rule was dropped in version 3.4.0, because it promoted an increase in laziness which could lead to bugs.\n\nC408: Unnecessary ``\u003cdict/list/tuple\u003e`` call - rewrite as a literal.\n--------------------------------------------------------------------\n\nIt's slower to call e.g. ``dict()`` than using the empty literal, because the name ``dict`` must be looked up in the global scope in case it has been rebound.\nSame for the other two basic types here.\nFor example:\n\n* Rewrite ``dict()`` as ``{}``\n* Rewrite ``dict(a=1, b=2)`` as ``{\"a\": 1, \"b\": 2}``\n* Rewrite ``list()`` as ``[]``\n* Rewrite ``tuple()`` as ``()``\n\nC409-410: Unnecessary ``\u003clist/tuple\u003e`` passed to ``\u003clist/tuple\u003e``\\() - ``\u003cadvice\u003e``.\n------------------------------------------------------------------------------------\n\nRules:\n\n* C409 Unnecessary ``\u003clist/tuple\u003e`` passed to tuple() - ``\u003cadvice\u003e``.\n* C410 Unnecessary list passed to list() - ``\u003cadvice\u003e``.\n\nWhere ``\u003cadvice\u003e`` is either:\n\n* remove the outer call to ``\u003clist/tuple\u003e``\\()\n* rewrite as a ``\u003clist/tuple\u003e`` literal\n\nIt's unnecessary to use a list or tuple literal within a call to ``list`` or ``tuple``, since there is literal syntax for these types.\nFor example:\n\n* Rewrite ``tuple([1, 2])`` as ``(1, 2)``\n* Rewrite ``tuple((1, 2))`` as ``(1, 2)``\n* Rewrite ``tuple([])`` as ``()``\n* Rewrite ``list([1, 2])`` as ``[1, 2]``\n* Rewrite ``list((1, 2))`` as ``[1, 2]``\n* Rewrite ``list([])`` as ``[]``\n\nC411: Unnecessary list call - remove the outer call to list().\n--------------------------------------------------------------\n\nIt's unnecessary to use a ``list`` around a list comprehension, since it is equivalent without it.\nFor example:\n\n* Rewrite ``list([f(x) for x in foo])`` as ``[f(x) for x in foo]``\n\nC412: Unnecessary ``\u003cdict/list/set\u003e`` comprehension - 'in' can take a generator.\n--------------------------------------------------------------------------------\n\nThis rule was dropped in version 3.4.0, because it promoted an increase in laziness which could lead to bugs.\n\nC413: Unnecessary ``\u003clist/reversed\u003e`` call around sorted().\n-----------------------------------------------------------\n\nIt's unnecessary to use ``list()`` around ``sorted()`` as it already returns a list.\nIt is also unnecessary to use ``reversed()`` around ``sorted()`` as the latter has a ``reverse`` argument.\nFor example:\n\n* Rewrite ``list(sorted([2, 3, 1]))`` as ``sorted([2, 3, 1])``\n* Rewrite ``reversed(sorted([2, 3, 1]))`` as ``sorted([2, 3, 1], reverse=True)``\n* Rewrite ``reversed(sorted([2, 3, 1], reverse=True))`` as ``sorted([2, 3, 1])``\n\nC414: Unnecessary ``\u003clist/reversed/set/sorted/tuple\u003e`` call within ``\u003clist/set/sorted/tuple\u003e``\\().\n--------------------------------------------------------------------------------------------------\n\nIt's unnecessary to double-cast or double-process iterables by wrapping the listed functions within ``list``/``set``/``sorted``/``tuple``.\nFor example:\n\n* Rewrite ``list(list(iterable))`` as ``list(iterable)``\n* Rewrite ``list(tuple(iterable))`` as ``list(iterable)``\n* Rewrite ``tuple(list(iterable))`` as ``tuple(iterable)``\n* Rewrite ``tuple(tuple(iterable))`` as ``tuple(iterable)``\n* Rewrite ``set(set(iterable))`` as ``set(iterable)``\n* Rewrite ``set(list(iterable))`` as ``set(iterable)``\n* Rewrite ``set(tuple(iterable))`` as ``set(iterable)``\n* Rewrite ``set(sorted(iterable))`` as ``set(iterable)``\n* Rewrite ``set(reversed(iterable))`` as ``set(iterable)``\n* Rewrite ``sorted(list(iterable))`` as ``sorted(iterable)``\n* Rewrite ``sorted(tuple(iterable))`` as ``sorted(iterable)``\n* Rewrite ``sorted(sorted(iterable))`` as ``sorted(iterable)``\n* Rewrite ``sorted(reversed(iterable))`` as ``sorted(iterable)``\n\nC415: Unnecessary subscript reversal of iterable within ``\u003creversed/set/sorted\u003e``\\().\n-------------------------------------------------------------------------------------\n\nIt's unnecessary to reverse the order of an iterable when passing it into one of the listed functions will change the order again.\nFor example:\n\n* Rewrite ``set(iterable[::-1])`` as ``set(iterable)``\n* Rewrite ``sorted(iterable)[::-1]`` as ``sorted(iterable, reverse=True)``\n* Rewrite ``reversed(iterable[::-1])`` as ``iterable``\n\nC416: Unnecessary ``\u003cdict/list/set\u003e`` comprehension - rewrite using ``\u003cdict/list/set\u003e``\\().\n-------------------------------------------------------------------------------------------\n\nIt's unnecessary to use a dict/list/set comprehension to build a data structure if the elements are unchanged.\nWrap the iterable with ``dict()``, ``list()``, or ``set()`` instead.\nFor example:\n\n* Rewrite ``{a: b for a, b in iterable}`` as ``dict(iterable)``\n* Rewrite ``[x for x in iterable]`` as ``list(iterable)``\n* Rewrite ``{x for x in iterable}`` as ``set(iterable)``\n\nC417: Unnecessary ``map`` usage - rewrite using a generator expression/``\u003clist/set/dict\u003e`` comprehension.\n---------------------------------------------------------------------------------------------------------\n\n``map(func, iterable)`` has great performance when ``func`` is a built-in function, and it makes sense if your function already has a name.\nBut if your func is a ``lambda``, it’s faster to use a generator expression or a comprehension, as it avoids the function call overhead.\nFor example:\n\n* Rewrite ``map(lambda x: x + 1, iterable)`` to ``(x + 1 for x in iterable)``\n* Rewrite ``map(lambda item: get_id(item), items)`` to ``(get_id(item) for item in items)``\n* Rewrite ``list(map(lambda num: num * 2, nums))`` to ``[num * 2 for num in nums]``\n* Rewrite ``set(map(lambda num: num % 2 == 0, nums))`` to ``{num % 2 == 0 for num in nums}``\n* Rewrite ``dict(map(lambda v: (v, v ** 2), values))`` to ``{v : v ** 2 for v in values}``\n\nC418: Unnecessary ``\u003cdict/dict comprehension\u003e`` passed to dict() - remove the outer call to dict()\n--------------------------------------------------------------------------------------------------\n\nIt's unnecessary to use a ``dict`` around a dict literal or dict comprehension, since either syntax already constructs a dict.\nFor example:\n\n* Rewrite ``dict({})`` as ``{}``\n* Rewrite ``dict({\"a\": 1})`` as ``{\"a\": 1}``\n\nC419 Unnecessary list comprehension in ``\u003cany/all\u003e``\\() prevents short-circuiting - rewrite as a generator.\n-----------------------------------------------------------------------------------------------------------\n\nUsing a list comprehension inside a call to ``any()``/``all()`` prevents short-circuiting when a ``True`` / ``False`` value is found.\nThe whole list will be constructed before calling ``any()``/``all()``, potentially wasting work.part-way.\nRewrite to use a generator expression, which can stop part way.\nFor example:\n\n* Rewrite ``all([condition(x) for x in iterable])`` as ``all(condition(x) for x in iterable)``\n* Rewrite ``any([condition(x) for x in iterable])`` as ``any(condition(x) for x in iterable)``\n\nC420: Unnecessary dict comprehension - rewrite using dict.fromkeys().\n----------------------------------------------------------------------\n\nIt's unnecessary to use a dict comprehension to build a dict with all values set to the same constant.\nUse ``dict.fromkeys()`` instead, which is faster.\nFor example:\n\n* Rewrite ``{x: 1 for x in iterable}`` as ``dict.fromkeys(iterable, 1)``\n* Rewrite ``{x: None for x in iterable}`` as ``dict.fromkeys(iterable)``\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadamchainz%2Fflake8-comprehensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadamchainz%2Fflake8-comprehensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadamchainz%2Fflake8-comprehensions/lists"}