min()

Returns the smallest item in a collection, or the smallest of several arguments.

Returns
The smallest item.
On this page

Example

#
Python
print(min([72, 95, 64, 88]))
print(min(3, 9, 1))
print(min(["Python", "CSS", "HTML"], key=len))
print(min([], default="no data"))
Resultsaved result, you can check it
64
1
CSS
no data

Definition and usage

#

The min() function takes a collection, or several separate arguments, and returns the smallest item. It compares strings alphabetically, by character code.

The key parameter lets you choose the criterion, for example the shortest string (key=len), or the student with the fewest points (key=lambda s: s["points"]).

On an empty collection, min() raises a ValueError. The default parameter lets it return a chosen value instead.

Syntax

#
Syntax
min(iterable, key=None, default=…)
min(a, b, *args, key=None)

Parameters

#
  • iterable

    a collection

    The items to compare, or several separate arguments.
  • key

    function, optional

    The comparison criterion.
  • default

    optional

    The value returned for an empty collection.

Good practices

#
  • For data that may be empty, always pass default to avoid an exception.
  • The object with the smallest field value: min(students, key=lambda s: s["points"]).

Related entries

#

Spotted a mistake or missing an example? Write to us.