[Django] #23968: Use generator comprehension instead of list comprehension when possible

11 views
Skip to first unread message

Django

unread,
Dec 6, 2014, 4:16:56 PM12/6/14
to django-...@googlegroups.com
#23968: Use generator comprehension instead of list comprehension when possible
--------------------------------------+--------------------
Reporter: jdufresne | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
Small cleanup to use generator comprehension instead of list comprehension
when the called function can easily accept both. This avoids an extra list
instance, thus avoid the small overhead of the temporary list.

For example, prefer:

{{{
tuple(force_text(item) for item in lst)
}}}

over:

{{{
tuple([force_text(item) for item in lst])
}}}

Patch to follow. Fixes uses of the following functions throughout the
codebase.

{{{
dict()
tuple()
any()
all()
min()
max()
sorted()
list.extend()
list +=
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23968>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 6, 2014, 4:41:22 PM12/6/14
to django-...@googlegroups.com
#23968: Use generator comprehension instead of list comprehension when possible
-------------------------------------+-------------------------------------
Reporter: jdufresne | Owner: nobody
Type: | Status: new
Cleanup/optimization | Version: master
Component: Uncategorized | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jdufresne):

* cc: jon.dufresne@… (added)
* needs_better_patch: => 0
* has_patch: 0 => 1
* needs_tests: => 0
* needs_docs: => 0


Comment:

https://github.com/django/django/pull/3701

--
Ticket URL: <https://code.djangoproject.com/ticket/23968#comment:1>

Django

unread,
Dec 6, 2014, 5:07:48 PM12/6/14
to django-...@googlegroups.com
#23968: Use generator comprehension instead of list comprehension when possible
--------------------------------------+------------------------------------

Reporter: jdufresne | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by collinanderson):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/23968#comment:2>

Django

unread,
Dec 6, 2014, 11:53:56 PM12/6/14
to django-...@googlegroups.com
#23968: Use generator comprehension instead of list comprehension when possible
-------------------------------------+-------------------------------------
Reporter: jdufresne | Owner: nobody

Type: | Status: new
Cleanup/optimization | Version: master
Component: Uncategorized | Resolution:
Severity: Normal | Triage Stage: Ready for
Keywords: | checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by collinanderson):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/23968#comment:3>

Django

unread,
Dec 8, 2014, 7:59:35 AM12/8/14
to django-...@googlegroups.com
#23968: Use generator comprehension instead of list comprehension when possible
-------------------------------------+-------------------------------------
Reporter: jdufresne | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version: master
Component: Uncategorized | Resolution: fixed

Severity: Normal | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"4468c08d70b5b722f3ebd4872909e56580ec7d68"]:
{{{
#!CommitTicketReference repository=""
revision="4468c08d70b5b722f3ebd4872909e56580ec7d68"
Fixed #23968 -- Replaced list comprehension with generators and dict
comprehension
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23968#comment:4>

Django

unread,
Jun 1, 2017, 7:09:10 PM6/1/17
to django-...@googlegroups.com
#23968: Use generator comprehension instead of list comprehension when possible
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Uncategorized | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for

| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"2c69824e5ab5ddf4b9964c4cf9f9e16ff3bb7929" 2c69824e]:
{{{
#!CommitTicketReference repository=""
revision="2c69824e5ab5ddf4b9964c4cf9f9e16ff3bb7929"
Refs #23968 -- Removed unnecessary lists, generators, and tuple calls.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23968#comment:5>

Django

unread,
Nov 7, 2024, 7:38:09 AM11/7/24
to django-...@googlegroups.com
#23968: Use generator comprehension instead of list comprehension when possible
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Uncategorized | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"042b381e2e37c0c37b8a8f6cc9947f1a2ebfa0dd" 042b381e]:
{{{#!CommitTicketReference repository=""
revision="042b381e2e37c0c37b8a8f6cc9947f1a2ebfa0dd"
Refs #23968 -- Removed unnecessary list comprehension in
contrib.admin.helpers.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23968#comment:6>
Reply all
Reply to author
Forward
0 new messages