Skip to content

Use generators instead of list comprehension#4768

Open
CrystalPea wants to merge 1 commit intomainfrom
small_code_improvements
Open

Use generators instead of list comprehension#4768
CrystalPea wants to merge 1 commit intomainfrom
small_code_improvements

Conversation

@CrystalPea
Copy link
Contributor

to make tasks faster and more memory-efficient.

For example (Notification.id.in_(n.id for n in notifications) is twice as fast as a generator than it is as a list comprehension.

Generators are also more memory efficient.

To make tasks faster and more memory-efficient.

For example (Notification.id.in_(n.id for n in notifications) is twice
as fast as a generator than it is as a list comprehension.

Generators are also more memory efficient.
@CrystalPea CrystalPea force-pushed the small_code_improvements branch from d2c5ff4 to 05d0b90 Compare March 6, 2026 17:45

Notification.query.filter(
Notification.id.in_([n.id for n in notifications]),
Notification.id.in_(n.id for n in notifications),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SQLAlchemy source is too convoluted to work out exactly how this will behave but I would be very wary of inadvertent generator exhaustion here.

The documentation says to use a list (although it doesn’t say not to use a generator).

I’d consider carefully:

  • how much time/memory this is saving in the overall context of a request/task
  • the complexity cost and potential for introducing subtle errors
  • how good this function’s test coverage is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants