Skip to content

Use len()

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.

Replaces sum() with len where appropriate

if sum(1 for hat in hats) > 0:
self.shout("I have hats")
if len(hats) > 0:
self.shout("I have hats")

This simplification can be made where the sum() call is effectively just determining the length of a sized object. It can only be made where Sourcery can determine that hats supports the __len__() method.