Python min() function returns the smallest item in the iterable or the smallest of two or more arguments.
Python min() function syntax is:
min(iterable, *[, key, default]) min(arg1, arg2, *args[, key])
- If there is only one argument, it should be an iterable such as string, list, tuple etc. The smallest item in the iterable is returned.
- If two or more arguments are provided, smallest of them will be returned.
- We can specify
keyargument function to be used for identifying the smallest item. This is an optional argument and mostly used when arguments are custom objects.
defaultargument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided,
ValueErrorexception is raised.
- If multiple smallest element is found, then the first one is returned.
Python min() function examples
Let’s look at some examples of python min() function.
min() with string
When min() function is used with string argument, the character with minimum unicode value is returned.
s = 'abcC' print(min(s)) for c in s: print(c, 'unicode value =', ord(c))
C a unicode value = 97 b unicode value = 98 c unicode value = 99 C unicode value = 67
I am using ord() function to print the unicode code point of the characters in the string.
min() with tuple
tuple_numbers = (1, 2, 3, 4) print(min(tuple_numbers))
min of list
list_numbers = [1, 2, 3, -4] print(min(list_numbers))
min() of objects
When we want to use min() function with custom objects, we have to provide
key function argument to be used for comparing the objects.
class Data: id = 0 def __init__(self, i): self.id = i def __str__(self): return 'Data[%s]' % self.id def get_data_id(data): return data.id # min() with objects and key argument list_objects = [Data(1), Data(2), Data(-10)] print(min(list_objects, key=get_data_id))
If we don’t provide a key function as an argument, we will get the following error.
TypeError: '<' not supported between instances of 'Data' and 'Data'
min() with empty iterable and default value
min() function with multiple arguments
print(min(1, 2, 3, 4))
min() with arguments and key function
def str_length(s): return len(s) print(min('a', 'abc', 'b', key=str_length))
Notice that both 'a' and 'b' are the smallest arguments, so the first one 'a' is returned by the min() function.
min() with multiple iterables
x1 = [10, 20, 30] x2 = [5, 15, 40, 25] print(min(x1, x2, key=len))
[10, 20, 30]
If we don't provide key function as an argument, the output will be
[5, 15, 40, 25]. It's because the comparison will be done between the elements of the iterable elements one by one. When an element with the smaller value is found, the iterable with that element will be returned.
min() with multiple iterables of objects
x1 = [Data(10), Data(20), Data(30)] x2 = [Data(5), Data(15), Data(40), Data(25)] min_list = min(x1, x2, key=len) for x in min_list: print(x)
Data Data Data
Notice that with multiple arguments, iterables are treated as objects. If we don't specify key function, we will get error message as
TypeError: '<' not supported between instances of 'Data' and 'Data'. Earlier it worked with integer elements because they support > and < operators.
Python min() function helps us in identifying the smallest element in the iterable or smallest item from multiple arguments. It's useful because we can specify our own function to be used for comparison through key argument. It's the opposite of python max() function.
Reference: Official Documentation