Python min()

In this tutorial, we will learn about the Python min() function with the help of examples.

The min() function returns the smallest item in an iterable. It can also be used to find the smallest item between two or more parameters.

Example

numbers = [9, 34, 11, -4, 27]

# find the smallest number
min_number = min(numbers)
print(min_number)

# Output: -4

The min() function has two forms:

# to find the smallest item in an iterable
min(iterable, *iterables, key, default)

# to find the smallest item between two or more objects
min(arg1, arg2, *args, key)

1. min() with iterable arguments

1.1. min() Syntax

Here’s the syntax of the min() function

min(iterable, *iterables, key, default)

1.2. min() Parameters

  • iterable – an iterable such as list, tuple, set, dictionary, etc.
  • *iterables (optional) – any number of iterables; can be more than one
  • key (optional) – key function where the iterables are passed and comparison is performed based on its return value
  • default (optional) – default value if the given iterable is empty

1.3. min() Return Value

min() returns the smallest element from an iterable.

1.4. Example 1: Get the smallest item in a list

number = [3, 2, 8, 5, 10, 6]
smallest_number = min(number);

print("The smallest number is:", smallest_number)

Output

The smallest number is: 2

If the items in an iterable are strings, the smallest item (ordered alphabetically) is returned.

1.5. Example 2: The smallest string in a list

languages = ["Python", "C Programming", "Java", "JavaScript"]
smallest_string = min(languages);

print("The smallest string is:", smallest_string)

Output

The smallest string is: C Programming

In the case of dictionaries, min() returns the smallest key. Let’s use the key parameter so that we can find the dictionary’s key having the smallest value.

1.6. Example 3: min() in dictionaries

square = {2: 4, 3: 9, -1: 1, -2: 4}

# the smallest key
key1 = min(square)
print("The smallest key:", key1)    # -2

# the key whose value is the smallest
key2 = min(square, key = lambda k: square[k])

print("The key with the smallest value:", key2)    # -1

# getting the smallest value
print("The smallest value:", square[key2])    # 1

Output

The smallest key: -2
The key with the smallest value: -1
The smallest value: 1

In the second min() function, we have passed a lambda function to the key parameter.

key = lambda k: square[k]

The function returns the values of dictionaries. Based on the values (rather than the dictionary’s keys), the key having the minimum value is computed.

Few Notes:

  • If we pass an empty iterator, a ValueError exception is raised. To avoid this, we can pass the default parameter.
  • If we pass more than one iterators, the smallest item from the given iterators is returned.

2. min() without iterable

2.1. min() Syntax

Here’s the syntax of min() function

min(arg1, arg2, *args, key)

2.2. min() Parameters

  • arg1 – an object; can be numbers, strings, etc.
  • arg2 – an object; can be numbers, strings, etc.
  • *args (optional) – any number of objects
  • key (optional) – key function where each argument is passed, and comparison is performed based on its return value

Basically, the min() function can find the smallest item between two or more objects.

2.3. min() Return Value

min() returns the smallest argument among the multiple arguments passed to it.

2.4. Example 4: Find the minimum among the given numbers

result = min(4, -5, 23, 5)
print("The minimum number is:", result)

Output

The minimum number is -5