Default Argument to max, min

Download python eBook

Example

You can't pass an empty sequence into max or min:

min([])

ValueError: min() arg is an empty sequence

However, with Python 3, you can pass in the keyword argument default with a value that will be returned if the sequence is empty, instead of raising an exception:

max([], default=42)        
# Output: 42
max([], default=0)        
# Output: 0

Stats

Contributors: 4
2016-08-14
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook