Skip to content

Max/min Default

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.

Use max/min default argument instead of if statement

a = [1, 2, 3]
if a:
b = max(a)
else:
b = 0
a = [1, 2, 3]
b = max(a, default=0)

When using max/min over an iterable we often want to provide a default value.

Instead of setting this using an if statement we can use the default keyword argument of max/min directly. This makes the intent of the code much clearer and makes it much easier to read.