Python min()

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

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

To find the smallest item in an iterable, we use this syntax:

min(iterable, *iterables, key, default)

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

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.

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.

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

To find the smallest item between two or more parameters, we can use this syntax:

min(arg1, arg2, *args, key)

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.


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

If you need to find the largest item, you can use the Python max() function.