Manage empty list/invalid input when finding max/min value of list (Python)

In Python 3.4, a default keyword argument has been added to the min and max functions. This allows a value of your choosing to be returned if the functions are used on an empty list (or another iterable object). For example:

>>> min([], default="no elements")
'no elements'

>>> max((), default=999)
999

>>> max([1, 7, 3], default=999) # 999 is not returned unless iterable is empty
7

If the default keyword is not given, a ValueError is raised instead.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)