Skip to content

Replace append loops with list comprehensions #80220

Closed

Description

@lgeiger
mannequin
BPO 36039
Nosy @rhettinger, @pganssle, @remilapeyre, @tirkarthi, @lgeiger

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2019-02-20.17:05:51.740>
created_at = <Date 2019-02-19.16:05:09.669>
labels = ['3.8', 'library', 'invalid', 'performance']
title = 'Replace append loops with list comprehensions'
updated_at = <Date 2019-02-25.19:58:17.365>
user = 'https://github.com/lgeiger'

bugs.python.org fields:

activity = <Date 2019-02-25.19:58:17.365>
actor = 'lgeiger'
assignee = 'none'
closed = True
closed_date = <Date 2019-02-20.17:05:51.740>
closer = 'rhettinger'
components = ['Library (Lib)']
creation = <Date 2019-02-19.16:05:09.669>
creator = 'lgeiger'
dependencies = []
files = []
hgrepos = []
issue_num = 36039
keywords = []
message_count = 6.0
messages = ['335961', '335962', '335971', '335973', '336120', '336550']
nosy_count = 5.0
nosy_names = ['rhettinger', 'p-ganssle', 'remi.lapeyre', 'xtreak', 'lgeiger']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'performance'
url = 'https://bugs.python.org/issue36039'
versions = ['Python 3.8']

Activity

  1. lgeiger commented on Feb 19, 2019

    lgeigermannequin
    MannequinAuthor

    Lib uses loops to append to a new list in quite a few places. I think it would be better to replace those with list comprehensions.

    Benefits of this change:

    • List comprehensions are generally more readable than appending to a newly created list

    • List comprehensions are also a lot faster.
      Toy example:
      In [1]: %%timeit
      ...: l = []
      ...: for i in range(5000):
      ...: l.append(i)
      375 µs ± 1.73 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

      In [2]: %%timeit
      ...: l = [i for i in range(5000)]
      168 µs ± 1.08 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

    Possible drawbacks:

    • Refactoring can always introduce bugs and makes it harder to get meaningful output from git blame. In this case I think the diff is very manageable, making the changes easy to review.

    Personally, I think the codebase would benefit from this change both in terms of some small performance gains and maintainability. I'd be happy to make a PR to fix this.

  2. added
    3.8 (EOL)
    stdlib
    performance
    on Feb 19, 2019
  3. pganssle commented on Feb 19, 2019

    @pganssle
    Member

    I think I'm -1 on a general project or policy to replace all for-append loops with list comprehensions, but I'm generally positive about individual improvements to the code base, on a case-by-case basis.

    I can't speak for anyone but myself, but I imagine that if you can identify a few places where this sort of change would make a significant performance improvement, it would be well-received.

  4. tirkarthi commented on Feb 19, 2019

    @tirkarthi
    Member

    Agreed with @p-ganssle . Similar bpo-33152 in the past for the refactoring in timeit module to use list comprehension. Large refactorings like this could also make git blame little more difficult in places where this has less benefit like smaller lists for common cases. I think it's much better to identify hot code paths with potentially large lists that could benefit from this along with a benchmark that could be accepted by the core dev/maintainer of the module.

  5. remilapeyre commented on Feb 19, 2019

    remilapeyremannequin
    Mannequin

    In some places, using a lot is also a deliberate choice to improve readability.

    I think the boy scout rule is more appropriate for such changes.

  6. rhettinger commented on Feb 20, 2019

    @rhettinger
    Contributor

    I concur with all the other -1 comments and will mark this a closed.

    FWIW, we generally discourage sweeping across the library with minor rewrites. Guido articulated a principle of "holistic refactoring" where we make code improvements while working on the module as a whole. This helps makes sure that code changes are made in the context of a thorough understanding of what the module is trying to do. This also helps us reduce maintenance-induced-bugs where minor code adjustments create new bugs that weren't there before.

    Thank you for taking a look at the source and please continue to do so. If you find a specific case that is problematic, feel free to post that one particular case. That said, a better use of time is to take one of the many open tracker issues, research it, and propose a fix.

  7. lgeiger commented on Feb 25, 2019

    lgeigermannequin
    MannequinAuthor

    Thank you very much for your thorough explanation, totally makes sense.

  8. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.8 (EOL)performancestdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions