Python min() With Examples

Python min() function returns the smallest item in the iterable or the smallest of two or more arguments.

Python min()

Python min() function syntax is:

  • 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 key argument function to be used for identifying the smallest item. This is an optional argument and mostly used when arguments are custom objects.
  • The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, ValueError exception 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.


I am using ord() function to print the unicode code point of the characters in the string.

min() with tuple

Output: 1

min of list

Output: -4

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.

Output: Data[-10]

If we don’t provide a key function as an argument, we will get the following error.

min() with empty iterable and default value

Output: 20

min() function with multiple arguments

Output: 1

min() with arguments and key function

Output: a

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

Output: [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


Notice that with multiple arguments, iterables are treated as objects. If we don’t specify key function, we will get error message as TypeError: '. Earlier it worked with integer elements because they support > and Summary

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.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation


By admin

Leave a Reply

%d bloggers like this: