Skip to content

Simplify-Constant-Sum

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Simplify constant sum() call

sum(1 for book in books if book.author == "Terry Pratchett")
sum(bool(book.author == "Terry Pratchett") for book in books)

As sum add the values it treats True as 1, and False as 0. We make use of this fact to simplify the generator expression inside the sum call.